Truncate Table - (PostgreSQL Table)
The TRUNCATE TABLE
command is a PostgreSQL command that allows you to quickly remove all the rows from a table without the need for a DELETE
statement. In this tutorial, we'll show you how to use the TRUNCATE TABLE
command to delete all data from a PostgreSQL table.
Syntax
TRUNCATE TABLE table_name;
table_name
: The name of the table to be truncated.
Example
Let's take a look at an example of the TRUNCATE TABLE
command in action:
TRUNCATE TABLE users;
In this example, all the data in the users
table will be deleted.
Explanation
The TRUNCATE TABLE
command deletes all data from a table by removing all rows. It is a quick way to clear a table of its data without the need for a DELETE
statement. Unlike the DELETE
statement, which deletes rows one at a time, TRUNCATE TABLE
simply removes all rows from the table in one go.
Additionally, the TRUNCATE TABLE
command resets the IDENTITY counter of a SERIAL
column to its original value.
Use
The TRUNCATE TABLE
command is used to quickly remove all data from a table without deleting the table itself. This is useful if you need to clear the data from a table but want to keep the table structure intact.
Important Points
- The
TRUNCATE TABLE
command cannot be undone once it has been executed. - When a table is truncated, all data is deleted. This means that any indexes, constraints or triggers associated with the table may also be deleted.
- If you need to delete specific rows from the table, you should use the
DELETE
statement instead ofTRUNCATE TABLE
.
Summary
In this tutorial, we discussed the TRUNCATE TABLE
command in PostgreSQL. We covered the syntax, example, output, explanation, use, and important points of using the TRUNCATE TABLE
command. With this knowledge, you can now use the TRUNCATE TABLE
command to quickly remove all data from a PostgreSQL table.