You can use the DELETE statement to delete records from a table. This topic describes how to use the statement.
DELETE statement
Syntax:
DELETE FROM table_name [ WHERE condition ];
| Parameter | Required | Description | Examples |
|---|---|---|---|
| table_name | Yes | The table from which data is to be deleted. | table1 |
| [ WHERE condition ] | No | The condition for deleting data. If no condition is specified, all data in the table is deleted. | where id=3 |
Note
If a table contains hundreds of thousands of records, a large transaction is generated, which may reduce the performance. We recommend that you specify the
WHEREclause to delete data in batches or use theTRUNCATE TABLEstatement.
Examples
Delete data from a table
Delete all rows meeting the o_w_id=2 condition from the ordr table.
DELETE FROM ordr WHERE o_w_id=2;
Delete all rows from the ordr table.
DELETE FROM ordr;
Delete data from a table in batches
For a table that contains millions of records, deleting all the records at a time may result in performance issues. We recommend that you delete the data in batches. In the following example, the data meeting the o_w_id < 100000, o_w_id < 200000, and o_w_id < 300000 conditions is deleted in batches.
DELETE FROM ordr WHERE o_w_id < 100000;
DELETE FROM ordr WHERE o_w_id < 200000;
DELETE FROM ordr WHERE o_w_id < 300000;
Clear a table
The TRUNCATE TABLE statement clears a table but retains its structure, including the partitions defined for the table. Logically, this statement is equivalent to the DELETE FROM statement that is used to delete all rows. In the following example, TRUNCATE TABLE is used to clear the ordr table:
obclient> TRUNCATE TABLE ordr;