YouTip LogoYouTip

Postgresql Truncate Table

# PostgreSQL TRUNCATE TABLE In PostgreSQL, `TRUNCATE TABLE` is used to delete the data from a table, but not the table structure itself. You can also use `DROP TABLE` to delete a table, but this command will delete the table structure along with the data. If you want to insert data again, you will need to recreate the table. `TRUNCATE TABLE` has the same effect as `DELETE`, but since it does not actually scan the table, it is much faster. Additionally, `TRUNCATE TABLE` can immediately free up table space without requiring a subsequent `VACUUM` operation, which is very useful for large tables. The PostgreSQL `VACUUM` operation is used to free up and reuse disk space occupied by updated/deleted rows. ### Syntax The basic syntax for `TRUNCATE TABLE` is: ```sql TRUNCATE TABLE table_name; ### Example Create a COMPANY table ((https://static.jyshare.com/download/company.sql)), with the following data: ```sql tutorialdb# select * from COMPANY; id | name | age | address | salary ----+-------+-----+-------------+-------- 1 | Paul | 32 | California | 20000 2 | Allen | 25 | Texas | 15000 3 | Teddy | 23 | Norway | 20000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 6 | Kim | 22 | South-Hall | 45000 7 | James | 24 | Houston | 10000 (7 rows) The following example uses `TRUNCATE TABLE` to clear the COMPANY table: ```sql tutorialdb=# TRUNCATE TABLE COMPANY; The result is as follows: ```sql tutorialdb=# SELECT * FROM CUSTOMERS; id | name | age | address | salary ----+------+-----+---------+-------- (0 rows)
← Postgresql TransactionPython3 Calendar β†’