更新日期:2024年09月11日
语法格式:
DELETE [FROM] [database_name.]table_name [tbl_alias] [WHERE where_definition]
其中,
关键字[FROM]和表别名[tbl_alias]是可选关键字。
一旦使用表别名,
则WHERE
子句中不得使用原表名。
注意:
Hive 引擎表开启了属性TBLPROPERTIES(”transactional”=”true”) 时方可执行
DELETE 操作。
对Hive 引擎表执行UPDATE 操作,影响行数均返回-1,这是由于HIVE 引擎本身
的限制。
示例中用到的表及数据:
CREATE TABLE t0 (id int,age int) CLUSTERED BY(id) INTO 2 BUCKETS STORED AS ORC
TBLPROPERTIES(' transactional ' = 'true') ENGINE = 'HIVE';
INSERT INTO t0 values(1,10),(2,20),(3,25),(4,30),(5,35),(6,40),(7,45),(8,50);
示例1:删除表中的数据。
gbase> SELECT * FROM t0;
+------+------+
| id | age |
+------+------+
| 8 | 50 |
| 6 | 40 |
| 4 | 30 |
| 2 | 20 |
| 7 | 45 |
| 5 | 35 |
| 3 | 25 |
| 1 | 10 |
+------+------+
8 rows in set
删除age 大于40 的数据。
gbase> DELETE FROM t0 WHERE t0.age > 40;
Query OK
GBase UP 产品手册 5 数据库管理指南
文档版本04(2021-04-21) 南大通用数据技术股份有限公司 762
gbase> SELECT * FROM t0;
+------+------+
| id | age |
+------+------+
| 6 | 40 |
| 4 | 30 |
| 2 | 20 |
| 5 | 35 |
| 3 | 25 |
| 1 | 10 |
+------+------+
6 rows in set
使用IN,删除age 值为10,20,30 的数据。
gbase> DELETE FROM t0 WHERE age IN ( 10,20,30);
Query OK
gbase> SELECT * FROM t0;
+------+------+
| id | age |
+------+------+
| 6 | 40 |
| 5 | 35 |
| 3 | 25 |
+------+------+
3 rows in set
删除全表数据。
gbase> DELETE FROM t0;
Query OK
gbase> SELECT * FROM t0;
Empty set
示例2:DELETE FROM...WHERE...IN (SELECT...FROM)
gbase> INSERT INTO t0 values(1,10),(2,20),(3,25),(4,30),(5,35),(6,40),(7,45),(8,50);
Query OK, 8 rows affected
Records: 8 Duplicates: 0 Warnings: 0
gbase> DELETE FROM t0 WHERE t0.age IN (SELECT age FROM t0);
Query OK
示例3:DELETE 语法省略FROM 关键字。
GBase UP 产品手册 5 数据库管理指南
文档版本04(2021-04-21) 南大通用数据技术股份有限公司 763
gbase> INSERT INTO t0 values(1,10),(2,20),(3,25),(4,30),(5,35),(6,40),(7,45),(8,50);
Query OK, 8 rows affected
Records: 8 Duplicates: 0 Warnings: 0
gbase> DELETE FROM t0 tt WHERE tt.age = 10;
Query OK
gbase> DELETE t0 tt WHERE tt.age=20;
Query OK
gbase> DELETE t0 WHERE age=30;
Query OK
查看删除后的数据:
gbase> SELECT * FROM t0;
+------+------+
| id | age |
+------+------+
| 8 | 50 |
| 6 | 40 |
| 7 | 45 |
| 5 | 35 |
| 3 | 25 |
+------+------+
5 rows in set
gbase> DELETE t0;
Query OK
gbase> SELECT * FROM t0;
Empty set
示例4:DELETE ...WHERE...
gbase> INSERT INTO t0 values(1,10),(2,20),(3,25),(4,30),(5,35),(6,40),(7,45),(8,50);
Query OK, 8 rows affected
Records: 8 Duplicates: 0 Warnings: 0
gbase> DELETE t0 WHERE age > 20;
Query OK, 1 row affected
gbase> SELECT * FROM t0;
+------+------+
| id | age |
+------+------+
GBase UP 产品手册 5 数据库管理指南
文档版本04(2021-04-21) 南大通用数据技术股份有限公司 764
| 2 | 20 |
| 1 | 10 |
+------+------+
2 rows in set
示例5:DELETE 语句使用表别名
gbase> INSERT INTO t0 values(1,10),(2,20),(3,25),(4,30),(5,35),(6,40),(7,45),(8,50);
Query OK, 8 rows affected
Records: 8 Duplicates: 0 Warnings: 0
-- WHERE 子句使用表别名
gbase> DELETE t0 tt where tt.age >35;
Query OK
-- 查看删除后的数据
gbase> SELECT * FROM t0;
+------+------+
| id | age |
+------+------+
| 1 | 10 |
+------+------+
1 rows in set
-- WHERE 子句使用原表名
gbase> DELETE t0 tt where t0.age = 10;
ERROR 1054 Unknown column 't0.age' in 'where clause'
使用CREATE TABLE 执行深层复制
该方法使用CREATE TABLE 语句创建原始表的副本,将原始表的数据填充至副本并重
命名副本,以完成原始表的复制。
在创建新表时,可以指定表以及列属性,比如主键。
操作步骤
执行以下步骤对表customer_t 进行深层复制。
步骤1 使用CREATE TABLE 语句创建表customer_t 的副本customer_t_copy。
postgres=# CREATE TABLE customer_t_copy
( c_customer_sk
integer,
c_customer_id
char(5),
c_first_name char(6),
c_last_name char(8)
) ;
步骤2 使用INSERT INTO…SELECT 语句向副本填充原始表中的数据。
postgres=# INSERT INTO customer_t_copy (SELECT * FROM customer_t);
步骤3 删除原始表。
postgres=# DROP TABLE customer_t;
步骤4 使用ALTER TABLE 语句将副本重命名为原始表名称。
postgres=# ALTER TABLE customer_t_copy RENAME TO customer_t;
----结束