Lesson 31 of 60 – TRUNCATE TABLE
52%

TRUNCATE TABLE in SQL

The TRUNCATE TABLE statement is used to remove all records from an existing table while keeping the table structure. It is useful when you want to empty a table completely but still want to keep the table for future use.

⚠️ Important: TRUNCATE removes all rows from the table. It does not use a WHERE clause, so always make sure that you really want to remove every record.

1. What is TRUNCATE TABLE?

TRUNCATE TABLE removes all records from a table while keeping the table structure.

TRUNCATE TABLE students;

All rows from the students table are removed.

2. Basic TRUNCATE Syntax

The basic syntax is:

TRUNCATE TABLE table_name;

Replace table_name with the name of the table you want to empty.

3. TRUNCATE a Students Table

Suppose a table named students contains many records.

TRUNCATE TABLE students;

All student records are removed, but the students table remains available.

4. TRUNCATE Keeps the Table Structure

TRUNCATE removes the rows but does not remove the table itself.

TRUNCATE TABLE students;

After TRUNCATE, the table can still be used for inserting new records.

5. TRUNCATE Removes All Rows

TRUNCATE does not remove selected records. It removes all records from the table.

TRUNCATE TABLE employees;

Every row in the employees table is removed.

Warning: TRUNCATE cannot be used to remove only one particular record.

6. TRUNCATE vs DELETE

DELETE can remove selected records using WHERE.

DELETE FROM students
WHERE city = 'Patna';

TRUNCATE removes all records.

TRUNCATE TABLE students;

7. TRUNCATE vs DROP

TRUNCATE removes all rows but keeps the table structure.

TRUNCATE TABLE students;

DROP TABLE removes both the data and the table structure.

DROP TABLE students;

8. TRUNCATE Does Not Use WHERE

TRUNCATE does not support a WHERE clause.

This is incorrect:

TRUNCATE TABLE students
WHERE city = 'Patna';

If you want to remove selected records, use DELETE.

DELETE FROM students
WHERE city = 'Patna';

9. TRUNCATE an Empty Table

You can truncate a table even if it already contains no records.

TRUNCATE TABLE students;

The table remains available for future use.

10. TRUNCATE a Large Table

TRUNCATE is commonly useful when you need to empty a table completely, such as a temporary or staging table.

TRUNCATE TABLE temporary_students;

The table remains available after the operation.

11. TRUNCATE and Table Structure

After TRUNCATE, the columns and their definitions remain.

TRUNCATE TABLE students;

DESCRIBE students;

The table structure can still be inspected.

12. TRUNCATE and AUTO_INCREMENT

In MySQL, TRUNCATE resets an AUTO_INCREMENT counter for a table.

TRUNCATE TABLE students;

Afterward, newly inserted records can start again from the initial AUTO_INCREMENT value.

13. TRUNCATE and Primary Key

TRUNCATE removes the rows but keeps the table definition, including its primary key definition.

TRUNCATE TABLE students;

The primary key structure remains part of the table.

14. TRUNCATE and Foreign Keys

Foreign key relationships can affect whether a table can be truncated.

TRUNCATE TABLE courses;

If other tables reference the table, the database system may restrict the operation depending on the relationship and database configuration.

15. TRUNCATE a Temporary Table

TRUNCATE can be useful for clearing temporary or staging tables.

TRUNCATE TABLE temp_students;

The table remains available after all its rows are removed.

16. TRUNCATE After Selecting a Database

First select the database you want to work with.

USE school_db;

TRUNCATE TABLE students;

The students table in school_db is emptied.

17. TRUNCATE Using Database Name

You can specify the database name along with the table name.

TRUNCATE TABLE school_db.students;

This identifies the students table inside the school_db database.

18. Check Data Before TRUNCATE

Before truncating a table, you can check how many records it contains.

SELECT COUNT(*) AS total_students
FROM students;

This helps you confirm how much data is currently stored.

19. Check Table Structure Before TRUNCATE

You can inspect the table before removing all its rows.

DESCRIBE students;

This shows the columns and their definitions.

20. TRUNCATE and Transactions

TRUNCATE has transaction behavior that depends on the database system. In MySQL, TRUNCATE TABLE is treated as a DDL operation and causes an implicit commit.

TRUNCATE TABLE students;

Therefore, do not assume that a TRUNCATE operation can always be rolled back like a normal DELETE operation.

21. TRUNCATE and Indexes

TRUNCATE removes the table's rows while keeping the table structure and associated index definitions.

TRUNCATE TABLE students;

The indexes remain part of the table structure.

22. TRUNCATE in Database Projects

TRUNCATE can be useful during development when you need to clear test data but keep the table structure.

TRUNCATE TABLE test_students;

The empty table can then be reused for new testing.

23. TRUNCATE in Testing

Testing systems often generate temporary data. TRUNCATE can quickly clear that data while keeping the table ready for another test.

TRUNCATE TABLE test_results;

The table remains available for the next test cycle.

24. TRUNCATE and Backup

Before truncating an important table, make sure the data is backed up if it may be needed later.

TRUNCATE TABLE students;
Warning: Do not truncate important production data without confirming that the data can be safely removed.

25. TRUNCATE vs DELETE vs DROP

The three commands have different purposes:

  • DELETE → removes selected rows and can use WHERE.
  • TRUNCATE → removes all rows but keeps the table structure.
  • DROP TABLE → removes the complete table.
DELETE FROM students WHERE student_id = 101;

TRUNCATE TABLE students;

DROP TABLE students;

26. Common TRUNCATE Mistake

A common mistake is using TRUNCATE when only a few records should be deleted.

TRUNCATE TABLE students;

This removes every row.

For selected records, use DELETE:

DELETE FROM students
WHERE student_id = 101;

27. Verify Data After TRUNCATE

You can use SELECT to verify that the table contains no rows.

TRUNCATE TABLE students;

SELECT *
FROM students;

The SELECT query should return no records if the table was successfully truncated.

28. Practical TRUNCATE Example

Suppose a temporary attendance table contains old test records.

TRUNCATE TABLE test_attendance;

All test attendance records are removed while the table remains ready for new data.

29. Complete TRUNCATE Example

Here is a simple workflow for checking and clearing a table.

SELECT COUNT(*) AS total_records
FROM test_students;

TRUNCATE TABLE test_students;

SELECT COUNT(*) AS total_records
FROM test_students;

The first query checks the records before TRUNCATE and the second checks them afterward.

30. Important TRUNCATE Rule

Remember that TRUNCATE removes every row from the table but keeps the table itself.

TRUNCATE TABLE students;

If you need to remove only specific records, use DELETE with a WHERE condition.

⚠️ Remember: TRUNCATE is an all-rows operation. Always confirm the table name before executing it.

📌 Key Points

  • TRUNCATE TABLE removes all records from a table.
  • It keeps the table structure.
  • TRUNCATE does not use a WHERE clause.
  • Use DELETE when you need to remove selected records.
  • Use DROP TABLE when you want to remove the complete table.
  • In MySQL, TRUNCATE resets the AUTO_INCREMENT counter.
  • TRUNCATE has database-specific transaction behavior.
  • Always verify the table before truncating important data.

🧠 Quick Quiz

Question: Which SQL statement removes all records but keeps the table structure?