0% found this document useful (0 votes)
25 views5 pages

PostgreSQL TRUNCATE TABLE

This document discusses how to use the PostgreSQL TRUNCATE TABLE statement to quickly delete all data from tables. It covers the basic syntax, using it with multiple tables, tables referenced by foreign keys, restarting sequences, and why it is more efficient than DELETE.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

PostgreSQL TRUNCATE TABLE

This document discusses how to use the PostgreSQL TRUNCATE TABLE statement to quickly delete all data from tables. It covers the basic syntax, using it with multiple tables, tables referenced by foreign keys, restarting sequences, and why it is more efficient than DELETE.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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

Introduction to PostgreSQL TRUNCATE TABLE


statement
To remove all data from a table, you use the DELETE statement without a WHERE
clause. However, when the table has numerous data, the DELETE statement is not
efficient. In this case, you can use the TRUNCATE TABLE statement.

The TRUNCATE TABLE statement deletes all data from a table very fast. Here’s the basic
syntax of the TRUNCATE TABLE statement:

TRUNCATE TABLE table_name;Code language: SQL (Structured Query Language) (sql)

In this syntax, you specify the name of the table that you want to delete data after the
TRUNCATE TABLE keywords.

Remove all data from multiple tables

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 :

TRUNCATE TABLE table_name


CASCADE;Code language: SQL (Structured Query Language) (sql)

PostgreSQL TRUNCATE TABLE statement examples


Let’s explore some examples of using the TRUNCATE TABLE statement.

1) Basic PostgreSQL TRUNCATE TABLE statement example

First, create a new table called products:

CREATE TABLE products(


id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10,2) NOT NULL DEFAULT 0
);Code language: SQL (Structured Query Language) (sql)

Second, insert some rows into the products table:

INSERT INTO products (name, price)


VALUES
('A', 19.99),
('B', 29.99),
('C', 39.99),
('D', 49.99)
RETURNING *;Code language: SQL (Structured Query Language) (sql)

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:

TRUNCATE TABLE products;Code language: SQL (Structured Query Language) (sql)

Output:

TRUNCATE TABLE

2) Using PostgreSQL TRUNCATE TABLE statement to delete all data


from multiple tables

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
);

INSERT INTO customers (name, phone) VALUES


('John Doe', '123-456-7890'),
('Jane Smith', '987-654-3210'),
('Robert Johnson', '555-123-4567')

RETURNING *;Code language: SQL (Structured Query Language) (sql)

Second, create a table called vendors and insert data into it:

CREATE TABLE vendors(


id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
phone VARCHAR(25) NOT NULL
);

INSERT INTO vendors (name, phone) VALUES


('ABC Electronics', '555-123-4567'),
('XYZ Supplies', '999-888-7777'),
('Tech Solutions Inc.', '111-222-3333')

RETURNING *;Code language: SQL (Structured Query Language) (sql)

Third, delete data from the customers and vendors tables using the TRUNCATE
TABLE statement:

TRUNCATE TABLE customers, vendors;Code language: SQL (Structured Query Language)


(sql)

3) Using PostgreSQL TRUNCATE TABLE statement to delete data from


a table referenced by a foreign key

First, create tables orders and order_details:

CREATE TABLE orders(


order_id SERIAL PRIMARY KEY,
customer_name VARCHAR(255) NOT NULL,
ordered_date DATE NOT NULL,
status VARCHAR(20) NOT NULL
);

CREATE TABLE order_items (


order_id INT NOT NULL,
item_id INT NOT NULL,
product_name VARCHAR(255) NOT NULL,
quantity INT NOT NULL,
FOREIGN KEY (order_id)
REFERENCES orders(order_id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (order_id, item_id)
);

INSERT INTO orders (customer_name, ordered_date, status)


VALUES
('John Doe', '2024-01-25', 'Processing'),
('Jane Smith', '2024-01-26', 'Shipped'),
('Bob Johnson', '2024-01-27', 'Delivered');

INSERT INTO order_items (order_id, item_id, product_name, quantity)


VALUES
(1, 1, 'A', 2),
(1, 2, 'B', 1),
(2, 1, 'C', 3),
(3, 1, 'D', 5),
(3, 2, 'E', 2);Code language: SQL (Structured Query Language) (sql)

Second, attempt to truncate data from the orders table:

TRUNCATE TABLE orders;Code language: SQL (Structured Query Language) (sql)

PostgreSQL issues the following error:

DETAIL: Table "order_items" references "orders".


HINT: Truncate table "order_items" at the same time, or use
TRUNCATE ... CASCADE.Code language: PHP (php)

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.

Third, truncate data from both orders and order_items tables:

TRUNCATE TABLE orders CASCADE;Code language: SQL (Structured Query Language) (sql)

PostgreSQL issues the following notice indicating that the order_items is also
truncated:

NOTICE: truncate cascades to table "order_items"


TRUNCATE TABLECode language: JavaScript (javascript)

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:

TRUNCATE TABLE table_name


RESTART IDENTITY;Code language: SQL (Structured Query Language) (sql)

For example, the following statement removes all rows from the products table and
resets the sequence associated with the id column:

TRUNCATE TABLE products


RESTART IDENTITY;Code language: SQL (Structured Query Language) (sql)
By default, the TRUNCATE TABLE statement uses the CONTINUE IDENTITY option. This
option does not restart the value in the sequence associated with the column in the table.

TRUNCATE TABLE statement and ON DELETE


trigger
Even though the TRUNCATE TABLE statement removes all data from a table, it does not
fire any ON DELETE triggers associated with the table.

To fire the trigger when the TRUNCATE TABLE statement executes, you need to define
BEFORE TRUNCATE and/or AFTER TRUNCATE triggers for that table.

TRUNCATE TABLE statement and transactions


The TRUNCATE TABLE is transaction-safe, meaning that you can place it within a
transaction.

Why TRUNCATE TABLE statement is more efficient


than the DELETE statement
The TRUNCATE TABLE statement is more efficient than the DELETE statement due to the
following main reasons:

 Minimal logging: The TRUNCATE TABLE statement doesn’t generate individual


row deletion logs. Instead, it deallocates entire data pages making it faster than
the DELETE statement.
 Fewer resources: The truncate operation is more lightweight than the delete
option because it doesn’t generate as much undo and redo information. It
releases storage space without scanning individual rows.
 Lower-level locking mechanism: The truncate operation often requires lower-
level locks and is less prone to conflicts with other transactions, which improves
overall system concurrency.

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.

You might also like