TRUNCATE TABLE

更新时间:
复制 MD 格式

Use the TRUNCATE TABLE statement to delete all data from a table or from specific partitions in an AnalyticDB for MySQL cluster. The table schema is preserved.

Syntax

  • Delete all data from a table:

    TRUNCATE TABLE db_name.table_name;
  • Delete data from specific partitions in a table:

    TRUNCATE TABLE db_name.table_name PARTITION partition_name[,...];

Parameters

Parameter

Description

db_name

The name of the database that contains the table.

table_name

The name of the table to truncate.

partition_name

The name of the partition to truncate. In AnalyticDB for MySQL, partition names are BIGINT values. Separate multiple partition names with commas. To get partition names, see Query partition names.

Query partition names

To list all partition names for a table, run:

SELECT partition_name FROM information_schema.partitions WHERE table_name = 'your_table_name' ORDER BY partition_name DESC LIMIT 100;

Usage notes

  • TRUNCATE TABLE cannot run while a database backup is in progress. If executed during a backup, an error is returned.

  • TRUNCATE TABLE cannot run while a BUILD task (a background data organization process) is running. Wait for the BUILD task to complete, then run the statement again. To check the status of a BUILD task, see View the state of a BUILD task.

Examples

  • Delete all data from the customer table in the adb_demo database:

    TRUNCATE TABLE adb_demo.customer;
  • Delete data from partitions 20170103, 20170104, and 20170108 in the customer table in the adb_demo database:

    TRUNCATE TABLE adb_demo.customer PARTITION 20170103,20170104,20170108;