Microsoft SQL Server

Chapter 6 - DML (Data Manipulation Language)

TRUNCATE

In Microsoft SQL Server, the TRUNCATE statement is used to quickly remove all rows from a table, similar to the DELETE statement. However, unlike DELETE, TRUNCATE is faster because it doesn’t log individual row deletions and doesn't allow filtering or use of the WHERE clause. It simply removes all data from a table and resets any identity columns to their initial values. For beginners, it’s important to understand that TRUNCATE is irreversible and should be used with caution.

Here’s a breakdown of how to use the TRUNCATE statement with examples:

  1. Basic Syntax of the TRUNCATE Statement:
    • The basic syntax for truncating a table is:
    TRUNCATE TABLE table_name;
  2. Steps for Using the TRUNCATE Statement:
    1. Specify the target table:
      • You use the TRUNCATE TABLE statement followed by the name of the table from which you want to remove all rows.
    2. Understand that TRUNCATE is non-filterable:
      • Unlike the DELETE statement, TRUNCATE doesn’t support the WHERE clause. It will remove all rows from the table.
  3. Example - Truncating the Sales Table:
    • Suppose you want to remove all data from the Sales table. Here’s how you would do it:
    TRUNCATE TABLE Sales;
    • This will remove every row from the Sales table and reset any identity columns, such as SaleID.
  4. Example - Truncating the Products Table:
    • If you want to remove all products from the Products table to reset the table:
    TRUNCATE TABLE Products;
    • This will clear all product records and reset the identity column for ProductID.
  1. Differences Between TRUNCATE and DELETE:
    • Speed:TRUNCATE is faster than DELETE because it doesn’t log individual row deletions.
    • Identity columns reset: If the table has an identity column (e.g., CustomerID), TRUNCATE resets the counter, while DELETE does not.
    • No WHERE clause:TRUNCATE does not allow you to filter which rows to remove, unlike DELETE, which lets you specify conditions.
  2. Best Practices:
    • Backup your data: Before using TRUNCATE, ensure that the data is backed up because it’s irreversible.
    • Use on tables with foreign keys cautiously:TRUNCATE cannot be used on tables referenced by foreign key constraints. In such cases, you must either remove the foreign keys or use the DELETE statement instead.
    • Avoid in production without careful planning: Since TRUNCATE removes all rows without logging individual deletions, it should be used cautiously in live environments.
  3. TRUNCATE vs. DELETE in a Nutshell:
    • TRUNCATE is faster but can’t be filtered.
    • TRUNCATE resets identity columns.
    • DELETE logs each deletion and supports filters via the WHERE clause.

By following these guidelines, beginners can effectively use the TRUNCATE statement in SQL Server for fast and efficient data removal. Just remember to be cautious as it removes all data from the table without any filters.

Tansy SQL Course - TRUNCATE - Video Thumbnail
Comments(0 comments)

Comments Not Found