PostgreSQL使用(二)

说明:本文介绍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语言,包括对数据库表的新增、更新和删除操作,参考下面视频:

相关推荐

  1. postgresql的基本使用

    2024-07-16 21:38:02       48 阅读
  2. PostgreSql 索引使用技巧

    2024-07-16 21:38:02       53 阅读
  3. Postgresql使用update

    2024-07-16 21:38:02       48 阅读
  4. PostgreSQL使用

    2024-07-16 21:38:02       21 阅读
  5. PostgreSQL使用(一)

    2024-07-16 21:38:02       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 21:38:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 21:38:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 21:38:02       58 阅读
  4. Python语言-面向对象

    2024-07-16 21:38:02       69 阅读

热门阅读

  1. 安全与认证:在Symfony中实现用户登录和权限管理

    2024-07-16 21:38:02       18 阅读
  2. redis面试题

    2024-07-16 21:38:02       22 阅读
  3. c++二叉搜索数模拟实现(代码)

    2024-07-16 21:38:02       17 阅读
  4. C#面 :dot net core工程里面有哪些常见的工程文件?

    2024-07-16 21:38:02       17 阅读
  5. docker部署项目命令

    2024-07-16 21:38:02       20 阅读
  6. ns3-gym入门(二):linear-mesh例子详解

    2024-07-16 21:38:02       16 阅读
  7. 数据结构与算法-09贪心算法&动态规划

    2024-07-16 21:38:02       16 阅读
  8. 访问者模式(大话设计模式)C/C++版本

    2024-07-16 21:38:02       17 阅读
  9. Logstash常用的filter四大插件

    2024-07-16 21:38:02       18 阅读
  10. RTOS中断与任务的同步

    2024-07-16 21:38:02       19 阅读
  11. 哈希表(知识点+leetcode)以及字符串哈希

    2024-07-16 21:38:02       21 阅读
  12. 运维检查:mysql表自增id是否快要用完

    2024-07-16 21:38:02       19 阅读