
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL TRUNCATE TABLE Statement
In SQL, sometimes we may need to remove all the data from a table but still want to keep the table structure for future use. In such cases, we use the TRUNCATE TABLE statement.
SQL TRUNCATE TABLE Statement
The SQL TRUNCATE TABLE command is used to remove all records from a table without deleting the table itself (i.e. empty a table). The table remains in the database, and you can insert new data into it later.
Syntax
The basic syntax of a TRUNCATE TABLE command in SQL is as follows:
TRUNCATE TABLE table_name;
Here:
- TRUNCATE TABLE: It is the SQL command to remove all records.
- table_name: It is the name of the table from which you want to remove all data.
Example
First let us create a table CUSTOMERS which can store the personal details of customers including their name, age, address and salary etc. as shown below:
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now, insert values into this table using the INSERT statement as follows:
INSERT INTO CUSTOMERS VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ), (2, 'Khilan', 25, 'Delhi', 1500.00 ), (3, 'Kaushik', 23, 'Kota', 2000.00 ), (4, 'Chaitali', 25, 'Mumbai', 6500.00 ), (5, 'Hardik', 27, 'Bhopal', 8500.00 ), (6, 'Komal', 22, 'Hyderabad', 4500.00 ), (7, 'Muffy', 24, 'Indore', 10000.00 );
The table will be created as follows â
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Following SQL TRUNCATE TABLE statement will remove all the records of the CUSTOMERS table:
TRUNCATE TABLE CUSTOMERS;
Verifying the Table After Truncation
After executing the above command, you can verify whether the table is empty using a SELECT statement as shown below:
SELECT * FROM CUSTOMERS;
If the table was truncated, the result will show no rows as shown below:
Empty set (0.00 sec)
SQL TRUNCATE vs DELETE
The TRUNCATE and DELETE commands may seem to be similar because they both remove data from a table, but there are some important differences between them. These differences are shown in the table below:
DELETE | TRUNCATE |
---|---|
Deletes one or more rows based on a condition using the WHERE clause. | Deletes all rows in the table without any condition. |
A DML (Data Manipulation Language) command. | A DDL (Data Definition Language) command. |
Requires manual COMMIT to save changes permanently. | Automatically commits changes; cannot be rolled back in most systems. |
Deletes rows one by one and logs each transaction. | Removes all data in one operation with minimal logging. |
Supports WHERE clause to filter which rows to delete. | Does not support WHERE clause. |
Locks each row during deletion. | Applies a table-level lock. |
Consumes more transaction log space. | Consumes less log space (only page deallocations are logged). |
Identity column values are not reset. | Identity values are reset to the original seed. |
Requires DELETE permission. | Requires ALTER permission. |
Slower for large datasets. | Much faster for large datasets. |
SQL TRUNCATE vs DROP
In SQL, both TRUNCATE and DROP are commands used to change the structure of a table. However, they are quite different in what they do and how they affect the database. The table below shows the main differences between them:
DROP | TRUNCATE |
---|---|
Removes the table completely from the database including data, structure, constraints, and indexes. | Removes all data from the table but keeps the table structure and constraints intact. |
A DDL (Data Definition Language) command. | A DDL (Data Definition Language) command. |
Frees up the table space from memory. | The table remains in the database and memory. |
All constraints, indexes, and definitions are removed permanently. | All constraints and indexes remain intact. |
Requires ALTER and CONTROL permissions. | Requires only ALTER permission. |
Faster than DELETE but slower than TRUNCATE. | Faster than both DROP and DELETE. |
Important Points on Using TRUNCATE TABLE
Following are some of the important points we should remember about the TRUNCATE TABLE statement:
- TRUNCATE quickly removes all rows from a table.
- You cannot use a WHERE clause with TRUNCATE because it removes everything.
- The table remains in the database and can still be used after truncation.
- In most databases (like MySQL), TRUNCATE also resets auto-increment values.
- Once executed, TRUNCATE cannot be undone unless your database supports transactions and you are using one.