Postgresql之学习笔记

1.安装包:https://download.csdn.net/download/weixin_41591572/10931275sql

2.扩展postgis:https://download.csdn.net/download/weixin_41591572/10884802数据库

好了,话很少说开启我postgresql 数据库学习之旅!服务器

第一章 postgresql 数据的相关入门知识

1.1背景

PostgreSQL 是一个免费的对象-关系数据库服务器(数据库管理系统),它在灵活的 BSD-风格许可证下发行。它提供了相对其余开放源代码数据库系统(好比 MySQL 和 Firebird),和专有系统(好比 Oracle、Sybase、IBM 的 DB2 和 Microsoft SQL Server)以外的另外一种选择。架构

1.2 数据库优缺点分析

后续。。。app

1.3架构

后续。。。函数

第二章  数据库经常使用使用知识

2.postgresql 数据类型

经常使用类型为:数值类型,字符串类型,日期时间类型post

数值数据类型

数字数据类型用于指定表中的数字数据。学习

名称 描述 存储大小 范围
smallint 存储整数,小范围 2字节 -32768 至 +32767
integer 存储整数。使用这个类型可存储典型的整数 4字节 -2147483648 至 +2147483647
bigint 存储整数,大范围。 8字节 -9223372036854775808 至 9223372036854775807
decimal 用户指定的精度,精确 变量 小数点前最多为131072个数字; 小数点后最多为16383个数字。
numeric 用户指定的精度,精确 变量 小数点前最多为131072个数字; 小数点后最多为16383个数字。
real 可变精度,不精确 4字节 6位数字精度
double 可变精度,不精确 8字节 15位数字精度
serial 自动递增整数 4字节 1 至 2147483647
bigserial 大的自动递增整数 8字节 1 至 9223372036854775807

字符串数据类型

String数据类型用于表示字符串类型值。.net

数据类型 描述
char(size) 这里size是要存储的字符数。固定长度字符串,右边的空格填充到相等大小的字符。
character(size) 这里size是要存储的字符数。 固定长度字符串。 右边的空格填充到相等大小的字符。
varchar(size) 这里size是要存储的字符数。 可变长度字符串。
character varying(size) 这里size是要存储的字符数。 可变长度字符串。
text 可变长度字符串。

日期/时间数据类型

日期/时间数据类型用于表示使用日期和时间值的列。开放源代码

名称 描述 存储大小 最小值 最大值 解析度
timestamp [ (p) ] [不带时区 ] 日期和时间(无时区) 8字节 4713 bc 294276 ad 1微秒/14位数
timestamp [ (p) ]带时区 包括日期和时间,带时区 8字节 4713 bc 294276 ad  
date 日期(没有时间) 4字节 4713 bc 5874897 ad 1微秒/14位数
time [ (p) ] [ 不带时区 ] 时间(无日期) 8字节 00:00:00 24:00:00 1微秒/14位数
time [ (p) ] 带时区 仅限时间,带时区 12字节 00:00:00+1459 24:00:00-1459 1微秒/14位数
interval [ fields ] [ (p) ] 时间间隔 12字节 -178000000年 178000000年 1微秒/14位数

一些其余数据类型

布尔类型:

名称 描述 存储大小
boolean 它指定truefalse的状态。 1字节

货币类型:

名称 描述 存储大小 范围
money 货币金额 8字节 -92233720368547758.08 至 +92233720368547758.07

几何类型:

几何数据类型表示二维空间对象。最根本的类型: - 造成全部其余类型的基础。

名称 存储大小 表示 描述
point 16字节 在一个平面上的点 (x,y)
line 32字节 无限线(未彻底实现) ((x1,y1),(x2,y2))
lseg 32字节 有限线段 ((x1,y1),(x2,y2))
box 32字节 矩形框 ((x1,y1),(x2,y2))
path 16+16n字节 封闭路径(相似于多边形) ((x1,y1),…)
polygon 40+16n字节 多边形(相似于封闭路径) ((x1,y1),…)
circle 24字节 <(x,y),r>(中心点和半径)

2.1 增删改查 以及经常使用知识

2.1.1insert  能够插入一行或者多行

-- union  或union all  用于合并两个或者多个select语句结果集。注意:union 内部的select列必须一致(数据类型、顺序、数量) ,数据显示的顺序是按照select顺序向下展现
-- 区别:二者基本类似,union 去重,union all 不去重
select id,bdzname from t_app_bdz where id='1'
union all 
select id,bdzname from anewtable where id='1'
--语法  单行
insert into tablename(column1,column2,...columnN)
values('val1','val2',...'valN')
--语法  多行
insert into tablename(column1,column2,...columnN)
values('val1','val2',...'valN'),
      ('val1','val2',...'valN'),
       ...
      ('val1','val2',...'valN')

2.1.2 delete  谨慎操做,若是不加where 条件将会删除表中全部的数据

--语法
delete from  tableName where [condiction]

2.1.3 update 必须使用where条件,不然将会更新表中的左右记录

--语法
update tableName set column1='val1',column2='val2',...,column='valN'

2.1.4 query 查询  必须加where,不然将拿出表中的全部记录,‘ * ’表明表中全部记录,'a '表后面加别名,同理字段后面也能够加别名。

--语法1
select 'column1','column2',...,'columnN' from tableName  where [condiction]
--语法2 例如
select a.* from user a where a.id='1'

-- 例如  必须加 a,b的关系 不然以下,会拿出a,id=82的一条数据,b中全部数据
select a.*,b.* from t_app_bdz a,t_app_jd b where a.id='82'

2.1.5 order by  用于排序,默认为升序。 desc降序,asc升序。

--语法
select column_list from  tableName  where [condinction] order by column1,column2,...,columnN asc|desc

2.1.6 group by用于对相同表结构的数据表 列进行分组,经过对多个收集记录数据,减小数据的输出冗余;

--语法
select column_list from tableName where [condiction] group by column1,column2,...,columnN
order by column1,column2,...,columnN

2.1.7 having  应用于group by 后面,至关于where,是为了知足必定条件下的特定行数据

--语法
select column_list from tableName where [condiction] group by column1,column2,...
having [condiction] order by column1,...
--例如
select name from class group by name having count(name)>2

2.1.8 一般和where 一块儿搭配使用的过滤器

1.and  2. or  3. and  & or  4.in 5. not in 6.like  7.between

--1,2,3很少说  in在 WHERE 子句中规定多个值
--语法 
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
--not in  不在where 条件内的值
--like 在 where子句中搜索列中的指定模式。
--'aa%'以aa开始的值; '%aa' 以aa结尾的数据  '%aa%' 包含aa的数据
SELECT column_name(s)
FROM table_name
WHERE column_name like 'aa%'

--BETWEEN 操做符在 WHERE 子句中使用,做用是选取介于两个值之间的数据范围。其中,值能够是文本,数值或者日期

SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2

2.1.9 alias  别名  和 distinct 

alias  简写 as  或者 不写 ,经常使用于表名,列名的别名

distinct 是对于表中重复数据的去重,在select字段后面用

--语法 
select distinct column_name as cn from table_name tn

2.2 union 与union all 的区别

-- union  或union all  用于合并两个或者多个select语句结果集。注意:union 内部的select列必须一致(数据类型、顺序、数量) ,数据显示的顺序是按照select顺序向下展现
-- 区别:二者基本类似,union 去重,union all 不去重

select id,bdzname from t_app_bdz where id='1'
union all 
select id,bdzname from anewtable where id='1'

2.3 left join 、right join 、full join、inner join 、join的区别

inner  join ==join== “ ,” 为内链接,即关联的一个或多个表中至少存在一条匹配的数据,没有返回空

left join 为左链接,即关联表中没有匹配信息,也会列出全部左表中的数据。这是通常左表为主表。

right join 为右链接,即关联表中没有匹配信息,也会列出全部右表中的数据。这是通常右表为主表。

full join 为全链接,即关联表中没有匹配信息,也会列出全部左和右表中的数据。

--语法 join 和inner join 
select column_name from  tablename1 join tablename2 where [condiction]

2.4case when  then  esle end 的用法

select country, sum( case when sex = '1' then   population else 0 end),  --男性人口

sum( case when sex = '2' then  population else 0 end)   --女性人口

from table_a   group by country;

2.5 select into

--  select into 用于表的备份文件或者记录存档  注意:会自动建立表,搭配in子句也能够从另外一个数据库中备份数据

select * into anewtable from t_app_bdz where id='2'
select * into anewtable in '备份库' from t_app_bdz where id='2'

2.6   create table 、alter  table、create DB 、create  index、drop index、

-- create table 、alter  table、create DB 、create  index、drop index、
-- create table 建立表
create table  atablename (
id varchar(32),
name varchar(32)
)
-- create database 建立数据库
CREATE DATABASE database_name
-- alter 用于添加、修改、删除列
alter table tablename
add column_name dataType
alter column column_name dateType
drop column column_name dataType  
-- create index  语句用于在表中建立索引。您能够在表中建立索引,以便更加快速高效地查询数据。用户没法看到索引,它们只能被用来加速搜索/查询。注释:更新一个包含索引的表须要比更新一个没有索引的表更多的时间,这是因为索引自己也须要更新。所以,理想的作法是仅仅在经常被搜索的列(以及表)上面建立索引。                                                                               
create index IndexName
ON tableName (LastName, FirstName desc)

2.7 null值

null值是遗漏的未知数据,默认的列值存放的为null值,若是在新增或者更新列时未赋值将会会存放null值;

注意:null值得处理方式与其余值处理方式不一样;null用于存放未知的或者不合适的值的占位符;null和0是不等价的

2.7.1 not null  、is null 、is not null 、null 函数、

not null  用做对于列的约束,强制约束列中包含值。若是新增,或更新不赋值将会报错

--用法
create table  person (
id varchar(32) not null,
name varchar(32) not null
)

null值的处理方式通常是用is null 或 is not  null 方式处理的

--is  null  
select  id,name from  person where  name is  null;
--is not null  
select  id,name from  person where  name is not null;

isnull() 相关函数: ifnull()、nvl()、isnull()、 coalesce()

isnull  

2.7经常使用相关函数

合并列,列转行

 

left 

right