TRUNCATE

Synopsis

Use the TRUNCATE statement to clear all rows in a table.

Syntax

  1. truncate ::= TRUNCATE [ TABLE ] { { [ ONLY ] name [ * ] } [ , ... ] }

truncate

TRUNCATE - 图1

Semantics

truncate

TRUNCATE [ TABLE ] { { [ ONLY ] name [ * ] } [ , … ] }

name

Specify the name of the table to be truncated.

  • TRUNCATE acquires ACCESS EXCLUSIVE lock on the tables to be truncated. The ACCESS EXCLUSIVE locking option is not yet fully supported.
  • TRUNCATE is not supported for foreign tables.

Examples

  1. yugabyte=# CREATE TABLE sample(k1 int, k2 int, v1 int, v2 text, PRIMARY KEY (k1, k2));
  1. yugabyte=# INSERT INTO sample VALUES (1, 2.0, 3, 'a'), (2, 3.0, 4, 'b'), (3, 4.0, 5, 'c');
  1. yugabyte=# SELECT * FROM sample ORDER BY k1;
  1. k1 | k2 | v1 | v2
  2. ----+----+----+----
  3. 1 | 2 | 3 | a
  4. 2 | 3 | 4 | b
  5. 3 | 4 | 5 | c
  6. (3 rows)
  1. yugabyte=# TRUNCATE sample;
  1. yugabyte=# SELECT * FROM sample;
  1. k1 | k2 | v1 | v2
  2. ----+----+----+----
  3. (0 rows)