Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

PostgreSQL Truncate Table

Truncate command removes all records from a table. But this command will not destroy the table's structure. When we apply truncate command on a table its Primary key is initialized.
Syntax:
truncate table table-name;
For example
postgres=# select * from item;
 item_no |  name  | cost 
---------+--------+------
       1 | laptop |   20
       2 | pen    |   10
       3 | paper  |    8
(3 rows)
--The below query will delete all the records of Student table.
postgres=# truncate table item;
TRUNCATE TABLE
--Table Structure(meta data) can't be delete ,its delete only records not metadata
postgres=# select * from item; 
 item_no | name | cost 
---------+------+------
(0 rows)
Difference between truncate and Delete command:
Delete command will delete all the rows from a table whereas truncate command re-initializes a table(like a newly created table) also operation will be done quickly compare to delete.

For eg. If you have a table with 10 rows and an auto_increment primary key, if you use delete command to delete all the rows, it will delete all the rows, but will not initialize the primary key, hence if you will insert any row after using delete command, the auto_increment primary key will start from 11. But in case of truncate command, primary key is re-initialized.

Comments

Popular posts from this blog

VMWARE WORKSTATION 3,4,5,6,7,8,9,10,11,12,14,15...etc LICENSE KEYS COLLECTION

How to find the server is whether standby (slave) or primary(master) in Postgresql replication ?

How to Configuration UDEV SCSI Rules In Oracle Linux 5, 6 and 7

aspell

Linux ctime,mtime,atime,cmin,amin,mmin