说明:本文介绍PostgreSQL的DML语言;
插入数据
-- 1.全字段插入,字段名可以省略
insert into tb_student values (1, '张三', '1990-01-01', 88.88);-- 2.部分字段插入,字段名必须写全
insert into tb_student (id, name) values (2, '李四');-- 3.插入的字段位置顺序可以不按表定义的顺序
insert into tb_student (name, id) values ('王五', 3);-- 4.批量插入(insert into)
insert into tb_student values (4, '赵六', '1990-01-01', 88.88), (5, '田七', '1990-01-01', 88.88);-- 5.批量插入(select),将查询表的数据插入到目标表中
insert into tb_student_goal select * from tb_student;-- 6.指定字段批量插入(select)
insert into tb_student_goal (id, name) select id, name from tb_student;
更新数据
-- 1.指定条件更新数据
update tb_student set name = '小何' where id = 1;-- 2.批量更新数据
update tb_student set score = 0;-- 3.将指定结果更新到对应字段
update tb_student set score = 50 + score where id = 1;
删除数据
-- 1.指定条件删除数据
delete from tb_student where id = 1;-- 2.指定范围删除数据
delete from tb_student where birthdate between '1990-01-01' and '1990-02-01';-- 3.清空表数据(DELETE)
delete from tb_student;-- 4.清空表数据(TRUNCATE)
truncate table tb_student;
DELETE和TRUNCATE清空数据表的区别如下:
总结
本文介绍了PostgreSQL的DML语言,包括对数据库表的新增、更新和删除操作,参考下面视频:
- 一天学完 PostgreSQL