PostgreSQL TRUNCATE TABLE
PostgreSQL TRUNCATE TABLE
Summary: in this tutorial, you will learn how to use PostgreSQL TRUNCATE TABLE
statement to quickly delete all data from large tables.
Create a free Postgres Database in 0.3 seconds on Neon. Ship faster with database
branching. Handle peaks efficiently with autoscaling.
Sponsored
The TRUNCATE TABLE statement deletes all data from a table very fast. Here’s the basic
syntax of the TRUNCATE TABLE statement:
In this syntax, you specify the name of the table that you want to delete data after the
TRUNCATE TABLE keywords.
To remove all data from multiple tables at once, you separate the tables by commas (,)
as follows:
TRUNCATE TABLE
table_name1,
table_name2,
...;Code language: SQL (Structured Query Language) (sql)
In this syntax, you specify the name of the tables that you want to delete all data after
the TRUNCATE TABLE keywords.
Remove all data from a table that has foreign key references
In practice, the table you want to delete all data often has foreign key references from
other tables.
By default, the TRUNCATE TABLE statement does not remove any data from the table that
has foreign key references.
To remove data from a table and other tables that have foreign key references the table,
you use CASCADE option in the TRUNCATE TABLE statement as follows :
Output:
id | name | price
----+------+-------
1 | A | 19.99
2 | B | 29.99
3 | C | 39.99
4 | D | 49.99
(4 rows)
Third, delete all data from the products table using the TRUNCATE TABLE statement:
Output:
TRUNCATE TABLE
First, create a table called customers and insert data into it:
CREATE TABLE customers(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
phone VARCHAR(25) NOT NULL
);
Second, create a table called vendors and insert data into it:
Third, delete data from the customers and vendors tables using the TRUNCATE
TABLE statement:
The reason is that the orders table is referenced by the order_items table. To truncate
both the orders and order_items tables at the same time, you can use the CASCADE
option.
TRUNCATE TABLE orders CASCADE;Code language: SQL (Structured Query Language) (sql)
PostgreSQL issues the following notice indicating that the order_items is also
truncated:
Note that the TRUNCATE TABLE statement uses the RESTRICT option by default to
prevent a table that is referenced by a foreign key from being truncated.
Restarting sequence
Besides removing data, you may want to reset the values of the identity column by
using the RESTART IDENTITY option like this:
For example, the following statement removes all rows from the products table and
resets the sequence associated with the id column:
To fire the trigger when the TRUNCATE TABLE statement executes, you need to define
BEFORE TRUNCATE and/or AFTER TRUNCATE triggers for that table.
Summary
Use the TRUNCATE TABLE statement to delete all data from a large table very
fast.
Use the CASCADE option to truncate a table that is referenced by foreign key
constraints.
The TRUNCATE TABLE deletes data but does not fire ON DELETE triggers. Instead,
it fires the BEFORE TRUNCATE and AFTER TRUNCATE triggers.
The TRUNCATE TABLE statement is transaction-safe.