PostgreSQL
docker run --name postgres \
-e POSTGRES_PASSWORD=123456 \
-p 5432:5432 \
-d postgres
感觉和mysql没啥区别
SQL语言
插入
insert into table values(id,name,sex....)
吧查询结果插入表中
insert into table_1 select * from table_2;
吧table_2 的内容 插入到 table_1 中
更新
update table set name = lf where id = 1;
删除
delete from table where id =3;
数据类型
类型 | 说明 | 补充 |
---|---|---|
smallint | -32768 ~ 32767 | |
int / integer | -21E ~ 21E | |
real | 浮点数 6位十进制精度 | |
numeric(m,n) | 任意精度 m 数据长度 n 小数点后精度 | 注意:超出位会自动四舍五入 |
time | 8字节 10:50:05 | |
date | 4字节 日期 1997-01-02 | |
timestamp | 日期时间 | 1998-01-02 10:20:02 |
char(n)/character(n) | 固定长度字符串,不足补空白 | |
varchar(n)/character carying(n) | 可变长度 | |
text | 变长字符串,无长度限制 | |
函数
select concat( '-',A,'-' ) from table;
结果; -A- 字符拼接
数值函数
函数 | 说明 |
---|---|
avg | 平均 |
count | 列的行数 |
max | 列最大值哦 |
min | 列最小值 |
sum | 列求和 |
字符串函数
函数 | 说明 |
---|---|
length(s) | 字符串长度 |
concat(s1,s2…) | 字符串合并 |
ltrim(s)/rtrim(s)/trim(s) | 删除字符串空格函数 |
replace(s,s1,s2) | 字符串替换函数 |
substring(s,n,len) | 获取子串函数 |
日期时间函数
函数 | 说明 |
---|---|
extract(type from d) | 获取日期指定值函数 |
current_date | 获取当前日期函数 |
current_time | 获取当天时间函数 |
now | 获取当前日期函数时间 |
select my_data,
extract( year/month/day from my_date ) 年/月/日
自定义函数
[视频][https://www.bilibili.com/video/BV1av411r7yB?p=12&spm_id_from=pageDriver&vd_source=529975faf79e2de2d708a6f0c8898290]
运算符
算数运算符
+、-、*、/、%
比较运算符
=、<>/!=、>=、<=、>、<
运算符 | 说明 |
---|---|
least | 在两个或者多个参数时,返回最小值 |
greatest | 在有两个或多个参数时,返回最大值 |
between and | 判断一个值是否落在两个值之间 注意 select 2 between 1 and 3 (是包含1,2,3) |
in | 判断是否时in 中的一个 |
like | 通配符匹配 |
逻辑运算符
not、and、or
运算符优先级
索引
索引名 | 说明 |
---|---|
B-Tree | 顺序索引 |
Hash | 简单比较 |
GiST | 一种索引架构 |
GIN | 反转索引,处理包含多个键的值 |
create index emp_name_index on employee(e_name); 创建索引 默认的是 BTree
drop index emp_name_index; 删除索引
简单查询
null值最前
nulls first/last。吧null 前置/后置
select xxx from xxx
order by name asc nulss first;
limit
获取5条数据 忽略前4条
select * from xx limit 5 offset 4;
子查询
exists 中的查询有结果就返回 true
select * from xx where exists(select * from xxx where xxx);