create table tb4(
id BIGSERIAL not null primary key,-- 不允许为空,&主键&自增
name varchar(16) not null, --允许为空
email varchar(32) null, -- 可以为空(默认)
age int default 3 -- 插入数据,不给值,默认值为3
);
\d
-- \d 表名
\d flow
TRUNCATE TABLE flow;
DROP TABLE flow;
\q
ALTER TABLE flow ADD age int;
ALTER TABLE flow DROP COLUMN age;
ALTER TABLE flow ALTER COLUMN date TYPE varchar;
Alter TABLE point alter column point TYPE geometry USING point ::geometry;
select st_astext(geo) from test;
alter table flow alter column age set not null;
ALTER TABLE flow ADD CONSTRAINT FlowUniqueConstraint UNIQUE(age);
-- 删除约束
ALTER TABLE flow DROP CONSTRAINT FlowUniqueConstraint ;
\d flow; # 查看主键的名称
ALTER TABLE flow DROP CONSTRAINT flow_pkey;
ALTER TABLE flow ADD CONSTRAINT FlowPrimaryKey PRIMARY KEY (ID);
-- 删除主键约束
ALTER TABLE flow DROP CONSTRAINT FlowPrimaryKey ;
select * from flow;
SELECT * FROM flow ORDER BY id ASC LIMIT 100;
SELECT * FROM flow ORDER BY id DESC LIMIT 100;
SELECT id,date FROM flow;
-- 修改列名称
SELECT id as n1,date as n2 FROM flow;
>
-- 查找年龄大于30
select * from info where age>30;
-- 查找id大于1
select * from info where id>1;
=
-- 查找id等于1
select * from info where id=1;
>=
-- 查找id大于等于1
select * from info where id>=1;
!=
-- 查找id不等于1
select * from info where id!=1;
between and
-- 查找2<=id<=4
select * from info where id between 2 and 4;
and
-- 查找name="wxy" 并且 age=19
select * from info where name="wxy" and age=19;
or
-- 查找name="wxy" 或者 age=49
select * from info where name="wxy" or age=49;
-- 查找name="wxy" 或者 email="[email protected]" 里面 age=49
select * from info where (name="wxy" or email="[email protected]") and age=49;
in
-- 查找id 等于1或4或6
select * from info where id in (1,4,6);
-- 查找id 在depart表中存在的id
select * from info where id in (select id from depart);
not in
-- 查找id不等于1或4或6
select * from info where id not in (1,4,6);
exists
-- 查找是否有select * from depart where id=5,如果有,就查询select * from info
select * from info where exists(select * from depart where id=5);
not exists
-- 查找是否有select * from depart where id=5,如果没有,就查询select * from info
select * from info where not exists(select * from depart where id=5);
子查询
-- 查找id>2 里面的age>10的信息
select * from (select * from info where id>2) as T where age>10;
is
-- 查询非空数据
SELECT * FROM flow WHERE "to" is not null;
limit
-- 取前五条
select * from info limit 5;
limit offset
-- 从第二个位置开始取,取三个,offset从0开始
select * from info limit 3 offset 2;
where
SELECT * FROM flow WHERE "from" = 1000;
like
用于模糊查询
%
【匹配表示0到多个】
select * from info where name like '%wxy';
_
【匹配一个】
select * from info where name like "_wxy";
count
-- 查询数据条数
SELECT COUNT(*) AS "flow" FROM flow;
sum
SELECT sum("from") AS "flow" FROM flow;
max
SELECT max("from") FROM flow;
min
SELECT min("from") FROM flow;
DISTINCT
【过滤重复值】SELECT DISTINCT id FROM depart;
group by
select age,max(id),min(id),count(id),sum(id),avg(id) from info group by age;
select age,count(1) from info group by age;
select depart_id,count(id) from info group by depart_id;
having
【先执行分组再判断条件】
select depart_id,count(id) from info group by depart_id having count(id)>2;
select age,max(id) from info group by age;
-- 只获取根据年龄分组的获取最大的id
select * from info where id in (select max(id) from info group by age);
-- 根据age分组,再查找count(id)>2
select age,count(id) from info group by age having count(id)>2;
-- 聚合条件,having放后面
select age,count(id) from info where id>4 group by age having count(id)>2;
select age,
count(id)
from info where id>2 group by age having count(id)>1 order by age desc limit 1;
- 要查询的表info
- 条件 id>2
- 根据age分组
- 对分组后的数据再根据聚合条件过滤 count(id)>1
- 根据age从大到小排序
- 获取第1条
获取自己想要的列
-- 只获取id,name两列
select id,name from info;
-- 将name列起别名为n
select id,name as n from info;
-- 增加一列123,所有值都为123
select id,name as n,123 from info;
-- 查找id,name,添加num列值为666,
-- 添加mid列值为depart表中id的最大值,
-- 添加nid列值为depart表中id的最小值
select id,
name,
666 as num,
(select max(id) from depart) as mid,
(select min(id) from depart) as nid,
age
from info;
-- 查找id,name,添加一列x1,info中的depart_id=depart中的id为条件,找depart表中的title作为x1的值
select id ,
name,
(select title from depart where depart.id=info.depart_id) as x1
from info;
select
id ,
name,
(select title from depart where depart.id=info.depart_id) as x1,
(select title from depart where depart.id=info.id) as x2
from info;
case when then end
-- 添加一列为v1,当depart_id=1时,将v1的值赋值为 "第1部门"
select id,
name,
case depart_id when 1 then '第1部门' end v1
from info;
case when then else end
-- 添加一列为v2,当depart_id=1时,将v2的值赋值为 "第1部门" ,否则赋值为 "其他"
select
id,
name,
case depart_id when 1 then '第1部门' else '其他' end v2
from info;
case when then ... when then ... when then ... else... end
-- case when then ... when then ... when then ... else... end
select id,
name,
case depart_id when 1 then '第一部门' end v1,
case depart_id when 2 then '第1部门' else '其他' end v2,
case depart_id when 1 then '第一部门' when 2 then '第2部门' else '其他' end v3,
case when age<18 then '少年' end v4,
case when age<18 then '少年' else '油腻男' end v5,
case when age<18 then '少年' when age<30 then '青年' else '油腻男' end v6
from info;
desc【降序】
select * from info order by age desc;
asc【升序】
select * from info order by age asc;
主表
主表 left outer join 从表 on 主表.x=从表.id
-- 左表连接,左边是主表
-- 右表连接,右边是主表
select * from info left outer join depart on info.depart_id=depart.id;
select info.id,info.name,info.email,depart.title from info left outer join depart on info.depart_id=depart.id;
从表
从表 right outer join 主表 on 主表.x = 从表.id
select info.id,info.name,info.email,depart.title from info right outer join depart on info.depart_id=depart.id;
列数需相同【union】
select id,title from depart
union
select id,name from info;
【union】自动去重
-- 注意:列的数据类型需要一致
select id,title from depart
union
select id,name from info;
获取所有【union all不去重】
select id from depart
union all
select id from info;
UPDATE covid19_policy SET id=1 WHERE id=0;
DELETE FROM covid19_policy WHERE ID = 2;
INSERT INTO flow (id, date, "from","to",flow_people) VALUES (0, '2020-01-01', 1,2,1);
-- 多行添加
INSERT INTO flow (id, date, "from","to",flow_people) VALUES (500000002, '2020-01-01', 1,2,1),(500000001, '2020-01-01', 1,2,1);
一对多
或多对一
关系
create table depart1(
id int not null primary key,
title varchar(16) not null
);
create table info1(
id int not null primary key,
name varchar(16) not null,
email varchar(32) not null,
age int,
depart_id int not null,
constraint fk_info1_depart1 foreign key (depart_id) references depart1(id)
) ;
-- 如果表结构创建好了,额外添加外键
alter table info1 add CONSTRAINT fk_info1_depart1 foreign key (depart_id) references depart1(id);
-- 删除外键
alter table info1 drop CONSTRAINT fk_info1_depart1;
多对多关系
create table boy(
id int not null primary key,
name varchar(16) not null
);
create table girl(
id int not null primary key,
name varchar(16) not null
);
create table boy_girl(
id int not null primary key,
boy_id int not null,
girl_id int not null,
constraint fk_boy foreign key (boy_id) references boy(id),
constraint fk_girl foreign key (girl_id) references girl(id)
);
-- 创建表之后添加外键
alter table boy_girl add constraint fk_boy foreign key (boy_id) references boy(id);
alter table boy_girl add constraint fk_girl foreign key (girl_id) references girl(id);
constraint fk_info1_depart1 foreign key (depart_id) references depart1(id)
-- 删除外键
alter table boy_girl drop CONSTRAINT fk_boy;
alter table boy_girl drop CONSTRAINT fk_girl;