SQL Server TRUNCATE TABLE

The TRUNCATE TABLE command in SQL Server allows you to rapidly and effectively remove every record from a table while maintaining the table structure. The TRUNCATE TABLE command is a more lightweight operation that is usually faster for large datasets than the DELETE statement, which eliminates rows one at a time and creates a larger transaction log.

TRUNCATE TABLE

It is a Data Definition Language (DDL) command called TRUNCATE TABLE statement that is used to deallocate a table’s data pages, thereby deleting every row. It is more effective for large tables since it does not log individual row deletions, which makes it faster than the DELETE statement. With the help of the TRUNCATE TABLE, We can remove all the records of the table at one time without any condition.

Prerequisites

To use TRUNCATE TABLE, you must first have the required authorizations. On the table, users must at least have the ALTER or CONTROL permissions. Furthermore, if a table is part of an indexed view or is referenced by a foreign key constraint from another table, it cannot be truncated.

Example Using TRUNCATE TABLE:

To understand the topic very effectively we need a table. Let’s create a table called Employee which consist EmployeeID, FirstName and LastName columns. If you don’t know How to Create Table in SQL Server then refer this.

After inserting some sample records our Employee table looks like:

Output:

Employee Table

Using TRUNCATE TABLE

Truncating the Employee Table

Here we use TRUNCATE TABLE command to remove all data from Employee table.

TRUNCATE TABLE Employee;


After performing the TRUNCATE Operation, The Employee Table Looks Like.

Querying the Table to Verify its Content

SELECT * FROM Employee;


Output:

Table After Truncate Operation

Explanation: After performing TRUNCATE Operation, all the records from Employee table has been deleted so when we try to print those records it will show 0 entries which means TRUNCATE has deleted all the records from table but preserves it schema.

Note: Since TRUNCATE TABLE eliminates all rows, it cannot be used with a WHERE clause, in respect to DELETE statement.

In conclusion, SQL Server’s TRUNCATE database function is a strong and effective technique to eliminate every entry from a database. It is especially helpful for huge datasets when speed is critical. Before using this command, you must, however, exercise caution and make sure that the required permissions are in place.

Benefits of TRUNCATE over Delete Command:

The TRUNCATE TABLE and DELETE commands in SQL Server used for similar purposes, but they have some key differences in terms of functionality and performance. Here are the benefits of using TRUNCATE TABLE over DELETE.

1. Performance:

Faster Execution, When deleting every row from a table, TRUNCATE TABLE typically performs faster than DELETE. This is because TRUNCATE is more efficient because it is a minimally logged operation that does not log individual row deletions.

2. Transaction Log Impact:

Reduced Log Space Usage: Compared to DELETE, TRUNCATE TABLE generates less transaction log activity. TRUNCATE utilizes less resources and produces less log data because it is a bulk operation that deallocates full data pages at once.

3. Locking Behavior:

Fewer Locks: Generally speaking, TRUNCATE TABLE requires less locks than DELETE, which lessens the possibility of contention and deadlock problems. On the table, TRUNCATE obtains a schema modification (Sch-M) lock, while DELETE might obtain individual row-level locks.

4. Remains Structure:

Identity Column Reset: DELETE does not reset identity columns to their seed value but TRUNCATE TABLE does. Using TRUNCATE, it guarantees that the identity values in any identity or auto-increment column in your table are reset.

5. Indexed Tables:

Less Impact on Indexes: The associated indexes are usually not as affected by using TRUNCATE TABLE as they are by using DELETE. Whereas DELETE could leave empty pages that still require management, TRUNCATE is a relatively straightforward operation that deallocates pages.

6. Permissions:

Simplified Permissions: TRUNCATE TABLE may require less permissions than DELETE in certain database systems. From a security standpoint, this can be helpful since it gives more precise control over data processing processes.

Despite these advantages, it’s crucial to remember that TRUNCATE TABLE might not be appropriate in every situation. For instance, if the table is part of an indexed view or there are foreign key restrictions referencing it, TRUNCATE cannot be utilized. Furthermore, TRUNCATE does not handle conditional deletion with a WHERE clause.

Conclusion

TRUNCATE TABLE is frequently a more effective option if your objective is to rapidly remove all rows from a table and reset identity columns, and you don’t need to take into account complicated scenarios like cascading deletes or trigger execution.

TRUNCATE TABLE – FAQs

1. What is the difference between DELETE and TRUNCATE in SQL Server?

There are some differences between the SQL DELETE and TRUNCATE statements when it comes to removing records from a table. TRUNCATE eliminates every row from a table without taking any conditions, whereas DELETE removes particular rows in accordance with a condition. Furthermore, compared to DELETE, TRUNCATE is quicker and requires fewer system resources however, it is not compatible with WHERE clauses.

2. Can TRUNCATE be rolled back in SQL Server?

In SQL Server, the TRUNCATE statement cannot be undone. A ROLLBACK command cannot reverse the modifications made after a TRUNCATE statement has been executed; the action is committed. TRUNCATE deletes all data from the selected table permanently, hence it should be used carefully, especially in a production setting.

3. Does TRUNCATE reset the Identity Columns in SQL Server?

Indeed, when you use the SQL Server TRUNCATE statement, all of the entries in the table are removed, and if the table has an identity column, it is reset to its original value. The DELETE statement, on the other hand, does not reset the identity column. Use DELETE rather than TRUNCATE if you wish to keep the values in the identity column.

4. Are there any constraints on using TRUNCATE in SQL Server?

Indeed, using the TRUNCATE command has some limitations. As an result, a foreign key constraint from another table cannot refer to the table. Participation of the table in an indexed view is restricted. There cannot be any declared triggers on the table. The ALTER or CONTROL permissions on the table are required.



Contact Us