OceanBase DELETE

2021-06-10 17:37 更新

描述

該語句用來刪除表中符合條件的行,包括單表刪除及多表刪除兩種方式。

格式

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} [, ...]
 

參數(shù)解釋

參數(shù)

描述

hint_options

指定 hint 選項。

tbl_name

指定需要刪除的表名。

partition_name

需要刪除表的對應分區(qū)名。

where_condition

刪除的表需要滿足的過濾條件。

order_expression_list

刪除的表的排序鍵列表。

row_count

刪除的表的行數(shù)指定,指定的值只能為整數(shù)。

table_references

多表刪除時指定的待選擇的表序列。

示例

示例表及數(shù)據(jù)基于以下定義:

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 列排序之后的第一行數(shù)據(jù)。

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)
  • 單表刪除:執(zhí)行刪除表 t2 的 p2 分區(qū)的數(shù)據(jù)。

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" 的數(shù)據(jù)。

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" 的數(shù)據(jù)。

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 分區(qū)中和 t1 表中 "t1.c1 = t2.c1" 的數(shù)據(jù)。

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);

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號