描述

该语句用来删除表中符合条件的行,包括单表删除及多表删除两种方式。

格式

  1. Single-Table-Delete Syntax:
  2. DELETE [hint_options] FROM tbl_name
  3. [PARTITION (partition_name,...)]
  4. [WHERE where_condition]
  5. [ORDER BY order_expression_list]
  6. [LIMIT row_count]
  7. Multiple-Table-Delete Syntax:
  8. DELETE [hint_options] tbl_name[.*] [, tbl_name[.*]] ...
  9. FROM table_references
  10. [WHERE where_condition]
  11. Or:
  12. DELETE [hint_options] FROM tbl_name[.*] [, tbl_name[.*]] ...
  13. USING table_references
  14. [WHERE where_condition]
  15. where_condition:
  16. expression
  17. order_expression_list:
  18. order_expression [, order_expression ...]
  19. order_expression:
  20. expression [ASC | DESC]
  21. limit_row_count:
  22. INT_VALUE
  23. table_references:
  24. {tbl_name | joined_table | table_subquery | select_with_parens} [, ...]

参数解释

参数

描述

hint_options

指定hint选项。

tbl_name

指定需要删除的表名。

partition_name

需要删除表的对应分区名。

where_condition

删除的表需要满足的过滤条件。

order_expression_list

删除的表的排序键列表。

row_count

删除的表的行数指定,指定的值只能为整数。

table_references

多表删除时指定的待选择的表序列。

示例

示例表及数据基于以下定义:

  1. OceanBase(admin@test)>create table t1(c1 int primary key, c2 int);
  2. Query OK, 0 rows affected (0.16 sec)
  3. OceanBase(admin@test)>select * from t1;
  4. +----+------+
  5. | c1 | c2 |
  6. +----+------+
  7. | 1 | 1 |
  8. | 2 | 2 |
  9. | 3 | 3 |
  10. | 4 | 4 |
  11. +----+------+
  12. 4 rows in set (0.06 sec)
  13. OceanBase(admin@test)>create table t2(c1 int primary key, c2 int) partition by key(c1) partitions 4;
  14. Query OK, 0 rows affected (0.19 sec)
  15. OceanBase(admin@test)>select * from t2;
  16. +----+------+
  17. | c1 | c2 |
  18. +----+------+
  19. | 5 | 5 |
  20. | 1 | 1 |
  21. | 2 | 2 |
  22. | 3 | 3 |
  23. +----+------+
  24. 4 rows in set (0.02 sec)
  • 单表删除:删除 “c1=2” 的行,其中 c1 列为表 t1 中的 Primary Key。
  1. OceanBase(admin@test)>DELETE FROM t1 WHERE c1 = 2;
  2. Query OK, 1 row affected (0.02 sec)
  3. OceanBase(admin@test)>select * from t1;
  4. +----+------+
  5. | c1 | c2 |
  6. +----+------+
  7. | 1 | 1 |
  8. | 3 | 3 |
  9. | 4 | 4 |
  10. +----+------+
  11. 3 rows in set (0.01 sec)
  • 单表删除:删除表 t2 的按照 c2 列排序之后的第一行数据。
  1. OceanBase(admin@test)>DELETE FROM t1 ORDER BY c2 LIMIT 1;
  2. Query OK, 1 row affected (0.01 sec)
  3. OceanBase(admin@test)>select * from t1;
  4. +----+------+
  5. | c1 | c2 |
  6. +----+------+
  7. | 2 | 2 |
  8. | 3 | 3 |
  9. | 4 | 4 |
  10. +----+------+
  11. 3 rows in set (0.00 sec)
  • 单表删除:执行删除表 t2 的 p2 分区的数据。
  1. OceanBase(admin@test)>DELETE FROM t2 PARTITION(p2);
  2. Query OK, 3 rows affected (0.02 sec)
  3. OceanBase(admin@test)>select * from t2;
  4. +----+------+
  5. | c1 | c2 |
  6. +----+------+
  7. | 5 | 5 |
  8. +----+------+
  9. 1 row in set (0.02 sec)
  • 多表删除:删除 t1,t2 表中 “t1.c1 = t2.c1” 的数据。
  1. OceanBase(admin@test)>DELETE t1, t2 FROM t1, t2 WHERE t1.c1 = t2.c1;
  2. Query OK, 3 rows affected (0.02 sec)
  3. OceanBase(admin@test)>select * from t1;
  4. +----+------+
  5. | c1 | c2 |
  6. +----+------+
  7. | 4 | 4 |
  8. +----+------+
  9. 1 row in set (0.01 sec)
  10. OceanBase(admin@test)>select * from t2;
  11. +----+------+
  12. | c1 | c2 |
  13. +----+------+
  14. | 5 | 5 |
  15. +----+------+
  16. 1 row in set (0.01 sec)
  • 多表删除:删除 t1,t2 表中 “t1.c1 = t2.c1” 的数据。
  1. OceanBase(admin@test)>DELETE FROM t1, t2 USING t1, t2 WHERE t1.c1 = t2.c1;
  2. Query OK, 4 rows affected (0.02 sec)
  3. OceanBase(admin@test)>select * from t1;
  4. +----+------+
  5. | c1 | c2 |
  6. +----+------+
  7. | 4 | 4 |
  8. +----+------+
  9. 1 row in set (0.01 sec)
  10. OceanBase(admin@test)>select * from t2;
  11. Empty set (0.01 sec)
  • 多表删除:删除 t2 表中的 p2 分区中和 t1 表中 “t1.c1 = t2.c1” 的数据。
  1. OceanBase(admin@test)>DELETE t2 FROM t1,t2 PARTITION(p2) WHERE t1.c1 = t2.c1;
  2. Query OK, 3 rows affected (0.02 sec)
  3. OceanBase(admin@test)>select * from t2;
  4. +----+------+
  5. | c1 | c2 |
  6. +----+------+
  7. | 5 | 5 |
  8. +----+------+
  9. 1 row in set (0.01 sec)
  • 对可更新视图v进行删除操作
  1. OceanBase(admin@test)>create view v as select * from t1;
  2. Query OK, 0 rows affected (0.07 sec)
  3. OceanBase(admin@test)>delete from v where v.c1 = 1;
  4. Query OK, 1 row affected (0.02 sec)
  5. OceanBase(admin@test)>select * from v;
  6. +----+------+
  7. | c1 | c2 |
  8. +----+------+
  9. | 2 | 2 |
  10. | 3 | 3 |
  11. | 4 | 4 |
  12. +----+------+
  13. 3 rows in set (0.01 sec)

注意事项

不管是多表删除还是单表删除都不支持直接对子查询进行删除操作,比如:

delete from (select * from t1);