Purpose
This statement is used to completely empty a specified table while retaining its structure, including the partition information defined in the table. Logically, this statement is equivalent to the DELETE FROM statement for removing all rows.
Limitations and considerations
The TRUNCATE TABLE statement differs from the DELETE FROM statement in the following ways:
The
TRUNCATEoperation deletes and recreates the table, which is much faster than deleting rows one by one, especially for large tables.The execution result of the
TRUNCATE TABLEstatement always shows the number of affected rows as0.When using the
TRUNCATE TABLEstatement, the table management program does not remember the last usedAUTO_INCREMENTvalue, but starts counting from the beginning.The
TRUNCATEstatement cannot be executed during transaction processing or table locking. If it is used, an error will be returned.As long as the table definition file is valid, the
TRUNCATE TABLEstatement can recreate the table as an empty table, even if the data or index files are damaged.
Privilege requirements
To execute the TRUNCATE TABLE statement, the current user must have the DROP privilege on the table. For more information about OceanBase Database privileges, see Privilege classification in Oracle mode.
Syntax
TRUNCATE [TABLE] [schema.]table_name;
Parameters
| Parameter | Description |
|---|---|
| schema. | Specifies the schema. If you omit schema, the table is assumed to be in the current schema. |
| table_name | Specifies the table name. |
Examples
Empty the tb1 table completely.
obclient> TRUNCATE TABLE tbl1;
Query OK, 0 rows affected