codecamp

OceanBase DELETE

描述

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

格式

Single-Table-Delete Syntax:
    DELETE [hint_options] FROM tbl_name
    [PARTITION (partition_name,...)]
    [WHERE where_condition]
    [ORDER BY order_expression_list]
    [LIMIT row_count]

Multiple-Table-Delete Syntax:
    DELETE [hint_options] tbl_name[.*] [, tbl_name[.*]] ...
    FROM table_references
    [WHERE where_condition]
Or:
    DELETE [hint_options] FROM tbl_name[.*] [, tbl_name[.*]] ...
    USING table_references
    [WHERE where_condition]
 
where_condition:
    expression

order_expression_list:
    order_expression [, order_expression ...]

order_expression:
    expression [ASC | DESC]

limit_row_count:
    INT_VALUE
  
table_references:
    {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

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

示例

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

obclient> create table t1(c1 int primary key, c2 int);
Query OK, 0 rows affected (0.16 sec)
obclient> select * from t1;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
+----+------+
4 rows in set (0.06 sec)

obclient> create table t2(c1 int primary key, c2 int) partition by key(c1) partitions 4;
Query OK, 0 rows affected (0.19 sec)
obclient> select * from t2;
+----+------+
| c1 | c2   |
+----+------+
|  5 |    5 |
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
+----+------+
4 rows in set (0.02 sec)
  • 单表删除:删除 “c1=2” 的行,其中 c1 列为表 t1 中的 Primary Key。

obclient> DELETE FROM t1 WHERE c1 = 2;
Query OK, 1 row affected (0.02 sec)

obclient> select * from t1;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    1 |
|  3 |    3 |
|  4 |    4 |
+----+------+
3 rows in set (0.01 sec)
  • 单表删除:删除表 t1 的按照 c2 列排序之后的第一行数据。

obclient> DELETE FROM t1 ORDER BY c2 LIMIT 1;
Query OK, 1 row affected (0.01 sec)

obclient> select * from t1;
+----+------+
| c1 | c2   |
+----+------+
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
+----+------+
3 rows in set (0.00 sec)
  • 单表删除:执行删除表 t2 的 p2 分区的数据。

obclient> DELETE FROM t2 PARTITION(p2);
Query OK, 3 rows affected (0.02 sec)

obclient> select * from t2;
+----+------+
| c1 | c2   |
+----+------+
|  5 |    5 |
+----+------+
1 row in set (0.02 sec)
  • 多表删除:删除 t1,t2 表中 "t1.c1 = t2.c1" 的数据。

obclient> DELETE t1, t2 FROM t1, t2 WHERE t1.c1 = t2.c1;
Query OK, 3 rows affected (0.02 sec)

obclient> select * from t1;
+----+------+
| c1 | c2   |
+----+------+
|  4 |    4 |
+----+------+
1 row in set (0.01 sec)

obclient> select * from t2;
+----+------+
| c1 | c2   |
+----+------+
|  5 |    5 |
+----+------+
1 row in set (0.01 sec)
  • 多表删除:删除 t1,t2 表中 "t1.c1 = t2.c1" 的数据。

obclient> DELETE FROM t1, t2 USING t1, t2 WHERE t1.c1 = t2.c1;
Query OK, 4 rows affected (0.02 sec)

obclient> select * from t1;
+----+------+
| c1 | c2   |
+----+------+
|  4 |    4 |
+----+------+
1 row in set (0.01 sec)

obclient> select * from t2;
Empty set (0.01 sec)
  • 多表删除:删除 t2 表中的 p2 分区中和 t1 表中 "t1.c1 = t2.c1" 的数据。

obclient> DELETE t2 FROM t1,t2 PARTITION(p2) WHERE t1.c1 = t2.c1;
Query OK, 3 rows affected (0.02 sec)

obclient> select * from t2;
+----+------+
| c1 | c2   |
+----+------+
|  5 |    5 |
+----+------+
1 row in set (0.01 sec)
  • 对可更新视图 v 进行删除操作

obclient> create view v as select * from t1;
Query OK, 0 rows affected (0.07 sec)

obclient> delete from v where v.c1 = 1;
Query OK, 1 row affected (0.02 sec)

obclient> select * from v;
+----+------+
| c1 | c2   |
+----+------+
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
+----+------+
3 rows in set (0.01 sec)

注意事项

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

delete from (select * from t1);

OceanBase CREATE VIEW
OceanBase DROP DATABASE
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

OceanBase 控制台指南

OceanBase ODC 使用指南

OceanBase Web 版 ODC

OceanBase 客户端版 ODC

OceanBase Connector/J 开发者指南

OceanBase 什么是OceanBase Connector/J

OceanBase SQL 参考(MySQL 模式)

OceanBase SQL 参考(Oracle 模式)

OceanBase 基本元素

OceanBase 数据库对象

OceanBase 函数

OceanBase 单行函数

OceanBase 返回数字的字符串函数

OceanBase 通用比较函数

OceanBase 编码解码函数

OceanBase SQL 调优指南

OceanBase 相关协议

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }