Lesson 32 of 60 – DROP / TRUNCATE TABLE
53%

MySQL DROP / TRUNCATE TABLE

MySQL provides DROP TABLE and TRUNCATE TABLE for removing table data or the entire table structure. Both are different from DELETE and should be used carefully.

Note: DROP TABLE removes the table itself, while TRUNCATE TABLE removes all rows but keeps the table structure.

1. What is DROP TABLE?

DROP TABLE removes an entire table from the database.

DROP TABLE students;

This removes the students table, including its data and structure.

2. Basic DROP TABLE Syntax

The basic syntax is:

DROP TABLE table_name;

After the statement executes successfully, the table no longer exists.

3. DROP TABLE Example

DROP TABLE employees;

This permanently removes the employees table from the current database.

4. DROP TABLE IF EXISTS

IF EXISTS prevents an error when the specified table does not exist.

DROP TABLE IF EXISTS students;

If the table exists, it is removed. If it does not exist, MySQL does not raise the usual missing-table error.

5. DROP Multiple Tables

You can drop multiple tables in one statement.

DROP TABLE students, teachers, courses;

All three specified tables are removed.

Warning: Make sure every listed table is safe to remove before executing the statement.

6. What is TRUNCATE TABLE?

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

TRUNCATE TABLE students;

The students table remains available for future INSERT operations.

7. Basic TRUNCATE Syntax

The basic syntax is:

TRUNCATE TABLE table_name;

All rows are removed, but the table definition remains.

8. TRUNCATE Example

TRUNCATE TABLE students;

All student records are removed while the students table itself remains.

9. DROP vs TRUNCATE

DROP TABLE TRUNCATE TABLE
Removes the table Keeps the table
Removes structure and data Removes all rows
Table must be recreated to use it again Table can be reused immediately

10. DELETE vs TRUNCATE

DELETE TRUNCATE
Can remove selected rows Removes all rows
Supports WHERE Does not support WHERE
Is a DML statement Is generally treated as DDL
Table structure remains Table structure remains

11. DELETE with WHERE

DELETE can remove selected records.

DELETE FROM students
WHERE course = 'Python';

Only Python students are deleted.

12. TRUNCATE Does Not Use WHERE

TRUNCATE removes all rows and does not support a WHERE condition.

TRUNCATE TABLE students;

You cannot write:

TRUNCATE TABLE students
WHERE course = 'Python';

For selective deletion, use DELETE with WHERE.

13. DROP vs DELETE

DELETE DROP TABLE
Removes rows Removes the table
Table remains Table is removed
Can use WHERE Cannot use WHERE

14. Checking Tables Before DROP

Before dropping a table, you can check the available tables.

SHOW TABLES;

You can then decide which table should be removed.

15. Checking Table Structure

Use DESCRIBE to inspect a table before making structural changes.

DESCRIBE students;

You can also use:

SHOW CREATE TABLE students;

These commands help you understand the current structure.

16. Checking Data Before TRUNCATE

Before truncating a table, check its data.

SELECT *
FROM students;

If you intentionally want to remove all rows, you can then use:

TRUNCATE TABLE students;

17. TRUNCATE and Table Structure

TRUNCATE removes the records but keeps the table definition.

TRUNCATE TABLE students;

DESCRIBE students;

The DESCRIBE command can still display the table structure after truncation.

18. AUTO_INCREMENT and TRUNCATE

For tables using an AUTO_INCREMENT column, TRUNCATE resets the AUTO_INCREMENT counter for an InnoDB table to its initial value.

TRUNCATE TABLE students;

After truncation, newly inserted rows can start again from the initial AUTO_INCREMENT value.

19. AUTO_INCREMENT and DELETE

DELETE removes rows but does not normally reset the AUTO_INCREMENT counter.

DELETE FROM students;

This is different from TRUNCATE, which resets the AUTO_INCREMENT counter for the table.

20. DROP and AUTO_INCREMENT

DROP TABLE removes the entire table, including its AUTO_INCREMENT definition.

DROP TABLE students;

If the table is later recreated, its AUTO_INCREMENT definition starts according to the new table definition.

21. DROP with Foreign Keys

Foreign key relationships can affect whether a table can be dropped. A table referenced by a foreign key may need the relationship handled first.

DROP TABLE courses;

If another table references courses through a foreign key, MySQL may prevent the operation depending on the relationship and constraints.

22. TRUNCATE with Foreign Keys

TRUNCATE can be restricted when foreign key relationships reference the table.

TRUNCATE TABLE courses;

When foreign key constraints are involved, check the relationships before attempting to truncate the table.

23. DROP Database vs DROP Table

DROP DATABASE removes an entire database, while DROP TABLE removes one table.

DROP TABLE students;

versus:

DROP DATABASE school_db;

The first removes one table. The second removes the entire database and its tables.

24. DROP TABLE IF EXISTS

Using IF EXISTS makes a DROP statement safer when the table may not exist.

DROP TABLE IF EXISTS students;

This is commonly used in development scripts and database setup scripts.

25. DROP and TRUNCATE in Development

During development, you may need to remove test data or recreate a table.

TRUNCATE TABLE students;

Use TRUNCATE when you want to keep the table structure but remove its rows.

DROP TABLE IF EXISTS students;

Use DROP when you want to remove the table itself.

26. Common DROP / TRUNCATE Mistakes

  • Using DROP when only the records needed to be removed
  • Using TRUNCATE when only selected rows should be deleted
  • Forgetting that DROP removes the table structure
  • Forgetting that TRUNCATE removes all rows
  • Trying to use WHERE with TRUNCATE
  • Ignoring foreign key relationships
  • Not checking the table before a destructive operation
  • Running DROP or TRUNCATE on production data without proper safeguards

27. Choosing DELETE, TRUNCATE, or DROP

Requirement Command
Delete selected rows DELETE
Remove all rows but keep table TRUNCATE TABLE
Remove the entire table DROP TABLE

28. Safe DROP / TRUNCATE Workflow

A safe workflow is:

  1. Confirm the current database.
  2. Check the available tables.
  3. Inspect the table structure.
  4. Check the data if required.
  5. Decide between DELETE, TRUNCATE, and DROP.
  6. Check relationships and dependencies.
  7. Execute the command carefully.
  8. Verify the result.
SELECT DATABASE();

SHOW TABLES;

DESCRIBE students;

29. Practical Student Table Example

Suppose a training institute wants to remove all old test records but keep the table structure.

SELECT *
FROM students;

TRUNCATE TABLE students;

DESCRIBE students;

The records are removed, while the students table remains available for new records.

30. Complete DROP / TRUNCATE Example

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100),
    course VARCHAR(100),
    age INT,
    fee DECIMAL(10,2)
);

INSERT INTO students
(name, course, age, fee)
VALUES
('Amit', 'Python', 22, 15000.00),
('Priya', 'Java', 21, 18000.00),
('Rahul', 'PHP', 24, 12000.00);

-- Check the data
SELECT *
FROM students;

-- Remove all rows but keep the table
TRUNCATE TABLE students;

-- Check the table structure
DESCRIBE students;

-- Remove the complete table
DROP TABLE IF EXISTS students;

TRUNCATE first removes all records while keeping the table. The final DROP TABLE removes the table itself.

📌 Key Points

  • DROP TABLE removes the complete table, including its structure and data.
  • TRUNCATE TABLE removes all rows but keeps the table structure.
  • DELETE can remove selected rows using WHERE.
  • TRUNCATE does not support a WHERE clause.
  • DROP TABLE IF EXISTS avoids an error when the table does not exist.
  • TRUNCATE resets the AUTO_INCREMENT counter for an InnoDB table.
  • DROP, TRUNCATE, and DELETE have different purposes.
  • Foreign key relationships can affect DROP and TRUNCATE operations.
  • Always check the database, table, data, and dependencies before destructive operations.
  • Use extra care with DROP and TRUNCATE on important or production databases.

🧠 Quick Quiz

Question: Which MySQL command removes all rows but keeps the table structure?