• Data Warehouse Service

dws
  1. Help Center
  2. Data Warehouse Service
  3. Developer Guide
  4. SQL Reference
  5. SQL Syntax
  6. TRUNCATE

TRUNCATE

Function

TRUNCATE quickly removes all rows from a set of tables. It has the same effect as an unqualified DELETE on each table, but it is faster since it does not actually scan the tables. This is most useful on large tables.

TRUNCATE TABLE has the same function as a DELETE statement with a WHERE clause, emptying a table.

TRUNCATE TABLE uses less system and transaction log resources as compared with DELETE.

  • DELETE deletes a row each time, and records the deletion of each row in the transaction log.
  • TRUNCATE TABLE deletes all rows in a table by releasing the data page storing the table data, and records the releasing of the data page only in the transaction log.

Comparison of TRUNCATE, DELETE, and DROP:

  • TRUNCATE TABLE deletes contents, releases space, but does not delete definitions.
  • DELETE TABLE deletes contents, but does not delete definitions nor release space.
  • DROP TABLE deletes contents and definitions, and releases space.

Syntax

  • TRUNCATE empties a table or set of tables.
TRUNCATE [ TABLE ] [ ONLY ] {table_name [ * ]} [, ... ]
    [ CONTINUE IDENTITY ] [ CASCADE | RESTRICT ];
  • Truncate the data in a partition.
ALTER TABLE [ IF EXISTS  ] { [ ONLY  ] table_name  
                           | table_name *  
                           | ONLY ( table_name )  } 
    TRUNCATE PARTITION { partition_name  
                       | FOR (  partition_value  [, ...] )  } ;

Parameter Description

  • ONLY

    If ONLY is specified, only the specified table is cleared. Otherwise, the table and all its subtables (if any) are cleared.

  • table_name

    Specifies the name (optionally schema-qualified) of a target table.

    Value range: an existing table name

  • CONTINUE IDENTITY

    Do not change the values of sequences. This is the default.

  • CASCADE | RESTRICT
    • CASCADE: automatically truncates all tables that have foreign-key references to any of the named tables, or to any tables added to the group due to CASCADE.
    • RESTRICT (default): refuses to truncate if any of the tables have foreign-key references from tables that are not listed in the command.
  • partition_name

    Indicates the partition in the target partitioned table.

    Value range: An existing partition name.

  • partition_value

    Specifies the value of the specified partition key.

    The value specified by ARTITION FOR can uniquely identify a partition.

    Value range: The partition key of the partition to be deleted.

    When the PARTITION FOR clause is used, the entire partition where partition_value is located is cleared.

Example

-- Create a table.
CREATE TABLE tpcds.reason_t1 AS TABLE tpcds.reason;

-- Clear the tpcds.reason_t1 table:
TRUNCATE TABLE tpcds.reason_t1;

-- Delete the table.
DROP TABLE tpcds.reason_t1;
-- Create a partitioned table:
CREATE TABLE tpcds.reason_p
(
  r_reason_sk integer,
  r_reason_id character(16),
  r_reason_desc character(100)
)PARTITION BY RANGE (r_reason_sk)
(
  partition p_05_before values less than (05),
  partition p_15 values less than (15),
  partition p_25 values less than (25),
  partition p_35 values less than (35),
  partition p_45_after values less than (MAXVALUE)
);

-- Insert data.
INSERT INTO tpcds.reason_p SELECT * FROM tpcds.reason;

-- Clear the p_05_before partition.
ALTER TABLE tpcds.reason_p TRUNCATE PARTITION p_05_before;

-- Clear the p_15 partition.
ALTER TABLE tpcds.reason_p TRUNCATE PARTITION for (13);

-- Clear the partitioned table.
TRUNCATE TABLE tpcds.reason_p;

-- Delete the tables.
DROP TABLE tpcds.reason_p;