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.
DROP TABLE removes an entire table from the database.
DROP TABLE students;
This removes the students table, including its data and structure.
The basic syntax is:
DROP TABLE table_name;
After the statement executes successfully, the table no longer exists.
DROP TABLE employees;
This permanently removes the employees table from the current database.
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.
You can drop multiple tables in one statement.
DROP TABLE students, teachers, courses;
All three specified tables are removed.
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.
The basic syntax is:
TRUNCATE TABLE table_name;
All rows are removed, but the table definition remains.
TRUNCATE TABLE students;
All student records are removed while the students table itself remains.
| 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 |
| 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 |
DELETE can remove selected records.
DELETE FROM students
WHERE course = 'Python';
Only Python students are deleted.
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.
| DELETE | DROP TABLE |
|---|---|
| Removes rows | Removes the table |
| Table remains | Table is removed |
| Can use WHERE | Cannot use WHERE |
Before dropping a table, you can check the available tables.
SHOW TABLES;
You can then decide which table should be removed.
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.
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;
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.
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.
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.
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.
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.
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.
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.
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.
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.
| Requirement | Command |
|---|---|
| Delete selected rows | DELETE |
| Remove all rows but keep table | TRUNCATE TABLE |
| Remove the entire table | DROP TABLE |
A safe workflow is:
SELECT DATABASE();
SHOW TABLES;
DESCRIBE students;
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.
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.
Question: Which MySQL command removes all rows but keeps the table structure?