Purpose
This statement is used to delete rows (data) that meet the specified conditions from a table.
Privilege requirements
To execute the DELETE statement, the current user must have the DELETE system privilege. For more information about OceanBase Database privileges, see Privilege classification in Oracle mode.
Syntax
DELETE [hint_options] FROM table_factor
[WHERE where_expression]
[{ RETURNING | RETURE } returning_exprs [ into_clause ]]
[LOG ERRORS [INTO] table_name REJECT LIMIT { integer | UNLIMITED }]
table_factor:
{table_name | table_subquery | '(' table_reference ')' }
returning_exprs:
projection_col_name [,projection_col_name ...]
into_clause:
[BULK COLLECT] INTO into_var_list
into_var_list:
{ USER_VARIABLE | ref_name } [, { USER_VARIABLE | ref_name }...]
Parameters
| Parameter | Description |
|---|---|
DELETE |
The keyword to execute the delete operation. |
hint_options |
Optional hint options used to optimize the execution plan of the query. |
| table_factor | Specifies the name of the table from which data is to be deleted (base table, updatable view, or special subquery).
NoticeA special subquery is similar to an updatable view and should not contain complex operators such as |
table_name |
The name of the target table from which data is to be deleted. |
table_subquery |
Optional subquery used to specify the source of the records to be deleted. |
table_reference |
The reference to the table, which can be the table name or other forms of table expressions. |
WHERE where_expression |
Optional condition used to specify the records to be deleted. |
RETURNING |
Optional keyword indicating that specific column values are returned after the delete operation. |
RETURN |
Same as RETURNING, another way to write it. |
returning_exprs |
A list of column names to be returned, which can include multiple column names. |
into_clause |
Optional statement used to store the returned values in variables. |
BULK COLLECT |
Indicates that the returned results are stored in variables using batch collection. |
into_var_list |
A list of variables where the returned values are stored, which can be user-defined variables or reference names. |
LOG ERRORS |
Optional statement indicating that errors are recorded during the delete operation. |
INTO |
Specifies the name of the target table for error records. |
REJECT LIMIT |
Specifies the maximum number of records allowed when an error occurs, which can be an integer or unlimited. |
Examples
Create a table named tbl1 and insert data.
obclient> CREATE TABLE tbl1(col1 INT PRIMARY KEY, col2 INT);
Query OK, 0 rows affected
obclient> INSERT INTO tbl1 VALUES(1,1),(2,2),(3,3),(4,4);
Query OK, 4 rows affected
Records: 4 Duplicates: 0 Warnings: 0
obclient> SELECT * FROM tbl1;
+------+------+
| COL1 | COL2 |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+------+------+
4 rows in set
Delete rows where
col1=2from thetbl1table, wherecol1is the primary key of thetbl1table.obclient>DELETE FROM tbl1 WHERE col1 = 2; Query OK, 1 row affected obclient>SELECT * FROM tbl1; +----+------+ | COL1 | COL2 | +----+------+ | 1 | 1 | | 3 | 3 | | 4 | 4 | +----+------+ 3 rows in setDelete rows directly from a subquery.
obclient> DELETE FROM (SELECT * FROM tbl1); Query OK, 4 rows affected obclient> SELECT * FROM tbl1; Empty setDelete rows with the
RETURNINGclause.obclient> DELETE FROM tbl1 RETURNING col1; +----+ | COL1 | +----+ | 1 | | 2 | | 3 | | 4 | +----+ 4 rows in set obclient>SELECT * FROM t1; Empty set