ITPub博客

首页 > 数据库 > 数据库开发技术 > openGauss学习笔记-13 openGauss 简单数据管理-DELETE语句

openGauss学习笔记-13 openGauss 简单数据管理-DELETE语句

原创 数据库开发技术 作者:shuchaoyang 时间:2023-04-01 08:00:00 0 删除 编辑

openGauss学习笔记-13 openGauss 简单数据管理-DELETE语句13.1 语法格式13.2 参数说明13.3 示例

openGauss学习笔记-13 openGauss 简单数据管理-DELETE语句

DELETE语句可以从指定的表里删除满足WHERE子句的行。如果WHERE子句不存在,将删除表中所有行,结果只保留表结构。

13.1 语法格式

DELETE FROM table_name 
      [WHERE condition];

13.2 参数说明

  • table_name

    目标表的名称(可以有模式修饰)。

    取值范围:已存在的表名。

  • condition

    一个返回Boolean值的表达式,用于判断哪些行需要被删除。

13.3 示例

创建表customer_t1_bak,表的结构、数据与customer_t1一致。

openGauss=# CREATE TABLE customer_t1_bak AS TABLE customer_t1;
INSERT 0 9

创建的表customer_t1_bak,数据如下:

openGauss=# SELECT * FROM customer_t1_bak;
c_customer_sk | c_customer_id | c_first_name | c_last_name | amount
---------------+---------------+--------------+-------------+--------
         3869 | hello         | Grace       |             |   1000
         3869 | hello         | Grace       |             |   1000
         3869 |               | Grace       |             |
         3869 | hello         |             |             |
         3869 | hello         |             |             |
              |               |             |             |
         6985 | maps         | Joes         |             |   2200
         9976 | world         | James       |             |   5000
         4421 | Admin         | Local       |             |   3000
(9 rows)

执行如下语句删除customer_t1_bak中c_customer_sk等于3869的职员。

openGauss=# DELETE FROM customer_t1_bak WHERE c_customer_sk = 3869;
DELETE 5

得到的结果如下,可以看到c_customer_sk = 3869的行已经被删除。

openGauss=# SELECT * FROM customer_t1_bak;
c_customer_sk | c_customer_id | c_first_name | c_last_name | amount
---------------+---------------+--------------+-------------+--------
              |               |             |             |
         6985 | maps         | Joes         |             |   2200
         9976 | world         | James       |             |   5000
         4421 | Admin         | Local       |             |   3000
(4 rows)

不指定WHERE语句时,默认删除整张表的数据,仅保留表结构。

openGauss=# DELETE FROM customer_t1_bak;
DELETE 4

得到结果如下。

openGauss=# SELECT * FROM customer_t1_bak;
c_customer_sk | c_customer_id | c_first_name | c_last_name | amount
---------------+---------------+--------------+-------------+--------
(0 rows)


👍 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向!


有什么吐槽或反馈意见,直接告诉我!

温馨提示:如出现二维码失效也可直接添加微信号:ysc13803862469。


原文作者:超哥的IT私房菜;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论
专注于IT系统集成、运维、架构二十年的IT职场老兵,拥有CCNA、OCA、OCP、PGCA、PGCE认证证书,公众号:超哥的IT私房菜,知识星球:超哥的IT私房菜,目前是多家IT厂商的咨询顾问。

注册时间:2021-08-27

  • 博文量
    552
  • 访问量
    303582