The TRUNCATE TABLE statement in SQL is a Data Definition Language (DDL) operation that labels a table's extents for DE allocation (empty for reuse). This procedure removes all data from a table easily, usually bypassing a variety of integrity checking mechanisms. It was included in the SQL: 2008 standard for the first time.

To remove all data from an existing table, use the SQL TRUNCATE TABLE order. You can also use the DROP TABLE command to delete an entire table. But Truncate will remove the entire table structure from the database, and you will need to recreate the table if you want to store any data.

Become an AI-powered Business Analyst

With Purdue University's Business Analysis ProgramExplore Now
Become an AI-powered Business Analyst

Syntax:

The basic syntax of a Truncate in SQL query is as follows:

TRUNCATE TABLE Tb_name;

Code:

Follow the steps below to perform Truncate in SQL.

1. Create the table customer with the cust_id, Cust_name, Cust_age and Cust_address

TruncateInSQL_1

2. For Truncate in SQL, insert the records to the following table customer_data with proper inputs.

Become The Highest-Paid Business Analysis Expert

With Business Analyst Master's ProgramExplore Now
Become The Highest-Paid Business Analysis Expert

Code:

Insert into customer_datavalues(1,’aaa’,20,’AAA’);

Insert into customer_datavalues(2,’bbb’,20,’BBB’);

Insert into customer_datavalues(3,’ccc’,20,’CCC’);

Insert into customer_datavalues(4,’ddd’,20,’DDD’);

Select * from customer_data;

TruncateInSQL_2

3. Truncate in SQL can be done now using the following code.

Code:

TRUNCATE TABLE customer_data;

TruncateInSQL_3.

4. For Truncate in SQL, once the table is truncated, the data from the table is removed. Here is how you can fetch the data from the table.

Code:

Select * from customer_data;

TruncateInSQL_4

When trying to show the records from that table, it shows a no data found error on the output screen. This is because it removes all the records from that particular table.

Even if the ROLLBACK is used, it doesn’t recover the table structure from the database.

Use the following code to know the difference in truncate activity.

Become an AI-powered Business Analyst

With Purdue University's Business Analysis ProgramExplore Now
Become an AI-powered Business Analyst

Code:

rollback;

After the rollback operation tries to show the record, it may show the same no data found on the output screen. But you can roll the table structure back. Except, the data can’t be retrieved.

TruncateInSQL_5

TruncateInSQL_6.

Arguments With Truncate in SQL:

 The TRUNCATE() function returns n with d decimal places truncated. It truncates n to 0 decimal places if you miss d. The function truncates the number n to d digits left to the decimal point if d is a negative number. MySQL recognizes the TRUNCATE() feature. MySQL, on the other hand, needs both n and d arguments.

Code:

TRUNCATE TABLE Animals

WITH Partitions (1 to 2, 4);

Before truncating, the table has data as shown below.

TruncateInSQL_7

After executing the truncate table query, the following output appears.

TruncateInSQL_8

Become The Highest-Paid Business Analysis Expert

With Business Analyst Master's ProgramExplore Now
Become The Highest-Paid Business Analysis Expert

Comparison Between Delete & Truncate:

The TRUNCATE TABLE has the following advantages over the DELETE statement:

  • The truncate command deletes all of a table's rows. Here, you can't use a Where clause.
  • A DDL order is what it is.
  • To delete all the data, the SQL Truncate command locks the table and page.
  • The truncate command does not add entries to the transaction log for each deleted row.
  • It is faster than the delete command.
  • The DELETE statement deletes rows one-by-one, and it records each deleted row in the transaction log. The TRUNCATE TABLE deletes data by relocating the data pages that were used to store the table data, and it records only the page relocating in the transaction log.
  • Each row in the table is locked for deletion when the DELETE statement is used with a row lock. TRUNCATE TABLE locks the table and page (including a schema (SCH-M) lock), but not each row
  • The table can still contain empty pages after a DELETE statement has been executed. Empty pages in a heap, for example, cannot be deallocated without first getting an exclusive (LCK M X) table lock. The table (heap) will contain several empty pages if the delete operation does not use a table lock. The delete operation for indexes can leave empty pages behind, but it will deal with these pages quickly.

Become an AI-powered Business Analyst

With Purdue University's Business Analysis ProgramExplore Now
Become an AI-powered Business Analyst

When Not to Use Truncate in SQL

The truncate query is used to remove the record from the tables. In some situations, the truncate query cannot change data records.

Following are the restriction to use the truncate query:

  • A foreign main restriction applies to them. A table with a self-referential foreign key can be truncated.
  • To become a part of an indexed view.
  • Transactional replication or merge replication are used to publish the data.
  • Temporally system-versioned.
  • An EDGE restriction is used to refer to them.
  • The EXPLAIN statement does not allow you to TRUNCATE TABLE.
  • TRUNCATE TABLE isn't allowed to be used inside a transaction.

To Truncate the Table in Large Size

Tables with more than 128 extents can be dropped or truncated in Microsoft SQL Server without requiring simultaneous locks on all of the extents necessary for the drop.

Permission for the Truncate

The ALTER permission on the table name is the bare minimum.

The table owner, members of the sysadmin fixed server function, the DB owner, and DBDDL admin fixed database roles have TRUNCATE TABLE permissions by default, and they are not transferable.

However, you can use the EXEC TRUNCATE TABLE statement to include the TRUNCATE TABLE statement within a module, such as a stored procedure, and give the module sufficient permissions.

Become The Highest-Paid Business Analysis Expert

With Business Analyst Master's ProgramExplore Now
Become The Highest-Paid Business Analysis Expert

Truncate the Table

To remove all the records from the table customer_data, you can include the select query before and after the truncate operation. With the help of this select statement easily understand it entirely removed the records from the table.

Code

Create table customer_data(cust_id number(10),cust_namevarchar(20),cust_age number(10), cust_addressvarchar(4));

Insert into customer_datavalues(1,’aaa’,20,’AAA’);

Select * from customer_data;

TruncateInSQL_9

Code

Truncate table customer_data;

Select * from customer_data;

TruncateInSQL_10

Become an AI-powered Business Analyst

With Purdue University's Business Analysis ProgramExplore Now
Become an AI-powered Business Analyst

Conclusion

Truncate is typically extremely fast, making it perfect for removing data from a temporary table.

In truncate in SQL, unlike drop table, which deletes the table's entire structure, keeps the table's structure for future usage.

Since deleting a table or database with the DROP statement cannot be reversed, it must be used carefully. Refer to our tutorial on the DROP Column to understand how to remove one or more columns from the table in SQL.

To extract rows from a table, SQL Delete and SQL Truncate command work well. This article has answered all of your questions about the remove and truncate SQL Server commands and clarified the differences between them. You should use the truncate command with caution since it eliminates all rows from a table.

If you want to gain expertise in SQL programming language, then Simplilearn's SQL Certification Training Course is for you. This SQL certification course provides you with everything you need to get started working with SQL databases and incorporating them into your applications. Learn how to organize your database correctly, write effective SQL statements and clauses, and maintain your SQL database for scalability. This course includes comprehensive coverage of SQL basics, comprehensive coverage of all relevant query tools and SQL commands, an industry-recognized course completion certificate, and lifetime access to self-paced learning.

Become The Highest-Paid Business Analysis Expert

With Business Analyst Master's ProgramExplore Now
Become The Highest-Paid Business Analysis Expert

While SQL is an old language, it is still very important today as businesses all over the world gather vast quantities of data in order to expand. SQL is regularly among the most-requested tech skills and mastering it will enhance your skillset.

Database and relationship management, Query tools and SQL commands, Aggregate functions, Group by clause, Table joins, Subqueries, Data manipulation, Transaction control, Views, and procedures are among the skills covered.

Have any questions for us? Leave them in the comments section of this article, and our experts will get back to you on them, as soon as possible!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449