OceanBase Database in MySQL mode allows you to clear a table by using the DELETE or TRUNCATE statement.
Prerequisites
The current user has the DELETE privilege for the target table.
DELETE statement
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 from two or more tables. |
Example
Clear the data in the dws_ny table.
Query the data in the
dws_nytable.obclient> SELECT * FROM dws_ny; +----+--------+------------+ | id | name | date | +----+--------+------------+ | 1 | Zhang San | 0000-00-00 | | 2 | Li Si | 0000-00-00 | +----+--------+------------+ 2 rows in setClear the data in the
dws_nytable.obclient> DELETE FROM dws_ny; Query OK, 2 rows affectedQuery the data in the
dws_nytable again.obclient> SELECT * FROM dws_ny; Empty set
TRUNCATE statement
Description
You can use this statement to completely clear a specified table but retain the table structure, including the partitions defined in the table. Logically, this statement is equivalent to the DELETE FROM statement that is used to delete all rows. To execute the TRUNCATE TABLE statement, you must have the DROP and CREATE privileges on the table. It is a DDL statement.
The TRUNCATE TABLE statement differs from the DELETE FROM statement in the following aspects:
TRUNCATE TABLE deletes a table and recreates one, which is much faster than deleting rows one by one.
The execution result of the TRUNCATE TABLE statement shows that 0 rows are affected.
When you execute the TRUNCATE TABLE statement, the table management program does not record the last AUTO_INCREMENT value. Instead, the program resets the value to zero.
The TRUNCATE TABLE statement cannot be executed during transactions or when the table is locked. Otherwise, an error is returned.
If the table definition file is valid, you can use the TRUNCATE TABLE statement to recreate the table as an empty table, even if the data or indexes are corrupted.
Syntax
TRUNCATE [TABLE] table_name;
Parameters
| Parameter | Description |
|---|---|
| table_name | The name of the table. |
Example
Clear the data in the t1 table.
Query the data in the
t1table.obclient> SELECT * FROM t1; +----+--------+------+ | id | name | age | +----+--------+------+ | 1 | Zhang San | 22 | | 2 | Li Si | 33 | +----+--------+------+ 2 rows in setClear the data in the
t1table.obclient> TRUNCATE t1; Query OK, 0 rows affectedQuery the data in the
t1table again.obclient> SELECT * FROM t1; Empty set