In today’s article, we will examine the PostgreSQL Truncate command, which deletes the data in the table regardless of any conditions.
Its general usage is as follows.
1 |
TRUNCATE TABLE table_name; |
An exclusive lock (other than the Truncate command is not allowed on the table) is set to the table where TRUNCATE is performed.
If you don’t want this to happen, you can use the DELETE clause.
Postgresql has given us some goodies when we truncate.
If there is an identity column in your table, options such as start from scratch when the table is deleted or continue from the last deleted data are offered.
You can do this with the following parameters.
RESTART IDENTITY: After the table is truncated, it is written at the end of the TRUNCATE clause to reset the identity value.
CONTINUE IDENTITY: Resumes the identity value from where it left off.
CASCADE: If the table has a connection with another table, it clears the data in all of them.
RESTRICT: If there is a reference key in the table, it cuts the reference key.
Let’s start our sample application.
First, let’s see the data in our table.
Let’s clear the data in the table with the TRUNCATE command.
1 |
TRUNCATE TABLE ogrenci_t1; |