Truncate Table in SQL - Empty the table

We learned that to delete all records from a table, use "delete" without a "where" condition.

Truncate Table in SQL:

We can also remove all the records from a table with "truncate table". Syntax:


 truncate table TABLENAME;


For example, we want to empty the table "books", we use:

truncate table books;


The "truncate table" statement empties the table (removes all records) and preserves the structure of the table.

The difference with "drop table" is that this statement drops the table, not only the records, "truncate table" empties it of records.


The difference with "delete" is the following, when using "delete", Oracle saves a copy of the deleted records and they are recoverable, with "truncate table" recovery is not possible because all the disk space occupied by the table is freed ; therefore "truncate table" is faster than "delete" (noticeable when the number of records is very large).

Comments

Popular posts from this blog

Data Consistency and Inconsistency - What is Difference

Cardinality in DBMS – max/minimum cardinality & examples

Relational Model in DBMS