Introduction to SQL TRUNCATE()
TRUNCATE in standard query language (SQL) is a data definition language (DDL) statement that is used to delete complete data from a database table without deleting the table itself. It frees up space or empties space in the table. However, we should note that TRUNCATE TABLE statements might not be roll backable in many SQL databases. Also being a DDL statement, TRUNCATE table statement does not require commit at each step, it automatically fires commit at the end of execution of the statement. Hence, we should be careful while using it.
We often get confused between TRUNCATE TABLE, DELETE TABLE and DROP TABLE statements in SQL. So before going ahead, let us first check and understand the difference between the three of them.
Differences between SQL TRUNCATE TABLE, SQL DELETE TABLE statement and SQL DROP TABLE statement
|DROP TABLE||TRUNCATE TABLE||DELETE TABLE|
|The SQL statement that removes all the data in the table and destroys the table itself.||The SQL statement that removes data rows from the table.||The SQL statement that removes data rows from the table.|
|It is a Data Definition Language(DDL) statement.||It is a Data Definition Language(DDL) statement.||It is a Data Manipulation Language(DML) statement.|
|We cannot use a WHERE condition.||We cannot use a WHERE condition.||We can use a WHERE condition.|
|We cannot perform a rollback operation to undo a DROP table statement.||We cannot perform a rollback operation to undo a TRUNCATE table statement.||We can perform a rollback operation to undo a DELETE table statement.|
|Table and all its relationships, index, privileges, etc are deleted permanently.||Table rows are deleted permanently.||Table rows are not deleted permanently.|
Syntax and Parameters
The basic syntax for using a SQL TRUNCATE TABLE statement is as follows :
TRUNCATE TABLE table_name;
The parameters used in the above syntax are :
Table_name: It is the name of the table whose records or rows you want to delete permanently.
How does the TRUNCATE TABLE statement work in SQL?
TRUNCATE TABLE statement in SQL, works by zeroing out a file in the database, i.e after running a TRUNCATE statement on an existing table, the table becomes empty and hence does not hold any row records. It resets the table to zero entries.
However, after truncating the table, its structure, columns, indexes, constraints, relationships, views, etc are preserved. The entire operation is like erasing data from the table but keeping the table intact.
TRUNCATE in Data Definition Language (DDL) is equivalent to DELETE in Data Manipulation Language (DML). The only difference between the two is that the latter can be rolled back, but the first one cannot be. However, TRUNCATE is faster than DELETE because it usually bypasses the transaction system. It is not logged (it can vary across SQL databases) and does not follow predicates and hence seems to be faster than DELETE operation. DELETE is a safer and slower operation.
Examples of SQL TRUNCATE()
Here are a few examples to explain the TRUNCATE TABLE statement in great detail.
Simple SQL query to illustrate the function of the TRUNCATE TABLE statement.
To understand the SQL TRUNCATE TABLE, let us consider a “customers” table. The data in the table looks like this.
SELECT * FROM public.customers
Next, let us run the TRUNCATE TABLE statement on the customer’s table to remove all its records. We can do so using the following SQL query.
TRUNCATE TABLE customers;
We can see in the figure below that the TRUNCATE TABLE statement has removed all the records in the customer’s table. However, all the columns, relationships, indexes, table structures have been kept safely.
SELECT * FROM customers;
SQL query to illustrate the difference between SQL DROP TABLE and TRUNCATE TABLE statements.
For this let us consider two tables “customer_details” and “students”. The table structure and the data in them look something like this. Records in the “Customer_details” table are as follows:
SELECT * FROM public.customers_details
Records in the “Students” table are as follows:
SELECT * FROM public.students
Next, we will be running the TRUNCATE TABLE on the customer_details table and DROP TABLE on the student’s table and then we will check the difference.
TRUNCATE TABLE customer_details;
DROP TABLE students;
We can observe from the images above that the DROP TABLE statement is faster than the TRUNCATE TABLE statement in SQL.
Now let us check what happened to the two tables after truncating and dropping respectively.
SELECT * FROM customer_details;
SELECT * FROM students;
From the above two images, we can observe that in the TRUNCATE statement the table structure is preserved, only the data/records in the table have been removed. Whereas in the case of the DROP TABLE statement, the entire table has been removed from the database. All its columns, indexes, relationships, privileges, etc. are removed.
TRUNCATE TABLE in SQL is a Data Definition Language (DDL) statement that empties an existing table by removing all the records while preserving table columns, privileges, indexes, views, constraints, relationships, etc. It is equivalent to but faster than the DELETE statement in SQL. However, unlike DELETE it cannot be rolled back.
This is a guide to SQL TRUNCATE(). Here we discuss the examples to explain the TRUNCATE TABLE statement in great detail along with the syntax and parameters. You can also go through our other suggested articles to learn more –