Description
You can use this statement to delete rows that meet the conditions from one or more tables.
Syntax
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} [, ...]
Parameters
| Parameter | Description |
|---|---|
| hint_options | The hint options. |
| tbl_name | The name of the table to be deleted. |
| partition_name | The name of the partition in the table to be deleted. |
| where_condition | The filtering condition that must be met for a table to be deleted. |
| order_expression_list | The list of sort keys for the table to be deleted. |
| row_count | The number of rows to be deleted from the table. The specified value must be an integer. |
| table_references | The sequence of tables to be selected when you delete data in two or more tables. |
Examples
Execute the following statement to create Table t1 and Table 2 and insert data into them:
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)
- Delete data in a single table: Delete rows where c1 = 2 from Table t1. Column c1 is the primary key column of Table t1.
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)
- Delete data in a single table: Delete the data in the first row sorted by Column c2 from Table t1.
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)
- Delete data in a single table: Delete data in Partition p2 from Table t2.
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)
- Delete data in two tables: Delete rows where t1.c1 = t2.c1 from Tables t1 and t2.
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)
- Delete data in two tables: Delete rows where t1.c1 = t2.c1 from Tables t1 and t2.
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)
- Delete data in two tables: Delete data from Partition p2 of Table t2 and rows where t1.c1 = t2.c1 from Table t1.
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)
- Delete an updatable View 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)
Note
Subqueries cannot be directly deleted regardless of whether you delete data from one or more tables. For example:
delete from (select * from t1);