SQL TRUNCATE TABLE: empty table

The TRUNCATE TABLE command in SQL is used to delete all rows from a table. It's similar to the DELETE command without a WHERE clause, but TRUNCATE TABLE is faster and uses fewer system and transaction log resources.

The syntax for the TRUNCATE TABLE command is quite simple:

TRUNCATE TABLE table_name;

So, for example, if you have a table named orders, you would truncate it like this:

TRUNCATE TABLE orders;

This would delete all rows from the orders table.

Here are some important points to remember about the TRUNCATE TABLE command:

  1. It cannot be used on tables that are referenced by a FOREIGN KEY constraint. You must remove the FOREIGN KEY constraint before truncating the table.

  2. It is a DDL (Data Definition Language) command, not a DML (Data Manipulation Language) command. This means it's a structure-level command like CREATE, ALTER, and DROP. Because of this, it does not generate individual row delete statements and does not log individual row deletes.

  3. It is faster and uses fewer system resources than the DELETE statement, which is why it's often used when you want to delete all rows from a table.

  4. It removes the data by deallocating the data pages used by the table. This reduces the resource overhead of logging the deletions, as well as the number of locks acquired; however, it means that TRUNCATE TABLE cannot be used to delete specific rows, while leaving others.

  5. It cannot be used with indexed views.

  6. Rollback is not available. Since TRUNCATE is a DDL command, it doesn't have the ability to rollback any changes as DML commands do with transactions.

  7. It resets the identity seed. If the table contains an identity column, the counter for that column is reset to the seed value defined for the column. If no seed was defined, the default value 1 is used.

Please be aware that the TRUNCATE TABLE command permanently removes all records from a table. It's a very powerful command and should be used with caution.

  1. Emptying a Table in SQL Using TRUNCATE:

    • Description: TRUNCATE TABLE is used to quickly remove all rows from a table.
    • Code Example:
      TRUNCATE TABLE example_table;
      
  2. TRUNCATE TABLE vs DELETE in SQL:

    • Description: TRUNCATE TABLE is faster than DELETE as it deallocates data pages, while DELETE removes rows one by one.
    • Code Example (DELETE):
      DELETE FROM example_table;
      
  3. How to Remove All Data from a Table in SQL:

    • Description: You can use either TRUNCATE TABLE or DELETE to remove all data from a table.
    • Code Example (TRUNCATE TABLE):
      TRUNCATE TABLE example_table;
      
    • Code Example (DELETE):
      DELETE FROM example_table;
      
  4. SQL TRUNCATE TABLE Example:

    • Code Example:
      TRUNCATE TABLE employees;
      
  5. Difference Between TRUNCATE TABLE and DROP TABLE:

    • Description: TRUNCATE TABLE removes all rows but retains the table structure, while DROP TABLE removes the entire table.
    • Code Example (DROP TABLE):
      DROP TABLE example_table;
      
  6. Truncating a Table in [Your Database System]:

    • Code Example (MySQL):
      TRUNCATE TABLE example_table;
      
    • Code Example (SQL Server):
      TRUNCATE TABLE example_table;
      
    • Code Example (PostgreSQL):
      TRUNCATE TABLE example_table;
      
    • Code Example (Oracle):
      TRUNCATE TABLE example_table;
      
  7. SQL TRUNCATE TABLE and Foreign Key Constraints:

    • Description: TRUNCATE TABLE is typically faster than DELETE even when foreign key constraints exist.
    • Code Example:
      TRUNCATE TABLE parent_table;
      
  8. Truncate Table Command in [Your Database System]:

    • Command (MySQL):
      TRUNCATE TABLE example_table;
      
    • Command (SQL Server):
      TRUNCATE TABLE example_table;
      
    • Command (PostgreSQL):
      TRUNCATE TABLE example_table;
      
    • Command (Oracle):
      TRUNCATE TABLE example_table;