TRUNCATE
Description
Statement use to empty a table or set of tables.
Grammar Syntax
TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ] [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
Click here to go to the PostgreSQL specification for this syntax.
Sample codes scenarios
Basic use without options
-- TABLE without descendants
TRUNCATE TABLE mytable;
-- TABLE with descendants
TRUNCATE TABLE table_with_descendants;
Using ONLY Option
This will generate that the first table
-- TABLE without descendants
TRUNCATE TABLE ONLY mytable;
-- TABLE with descendants
TRUNCATE TABLE ONLY table_with_descendants;
Without table keyword
-- TABLE without descendants
TRUNCATE mytable;
-- TABLE with descendants
TRUNCATE table_with_descendants;
For a list of multiple tables
Truncate TABLE mytable, table_with_descendants;
Using explicit `*` include descendants
-- TABLE without descendants
TRUNCATE TABLE mytable *;
-- TABLE with descendants
TRUNCATE TABLE table_with_descendants *;
Only option should apply to the first specified table when using lists
-- Case 1:
TRUNCATE ONLY mytable, table_with_descendants;
-- Case 2:
TRUNCATE ONLY table_with_descendants, mytable;
Use RESTART OPTION for tables
When using [RESTART IDENTITY] automatically restarts the sequences owned by columns of the truncated tables
TRUNCATE TABLE mytable, table_with_identity RESTART IDENTITY;
Use CONTINUE OPTION for tables
When using [CONTINUE IDENTITY] does not changes the values of the sequence
TRUNCATE TABLE ONLY table_with_identity CONTINUE IDENTITY;
Use CASCADE for tables
When a CASCADE OPTION is specified all tables related by a foreign key will be truncated
-- Case 1:
TRUNCATE mytable CASCADE;
-- Case 2:
TRUNCATE table_with_identity CASCADE;
Use RESTRICTED for tables
When a table has references a EWI is added to be reviewed.
TRUNCATE mytable RESTRICT;
TRUNCATE table_with_identity RESTRICT;
Last updated
Was this helpful?