MySQL DROP TABLE

DROP TABLE deletes a table completely: its structure and all of its data.

CREATE TABLE temp_students AS SELECT * FROM students;

DROP TABLE temp_students;
MySQL says

Query OK, 0 rows affected (0.02 sec)

Note: There is no undo for DROP TABLE. Take a backup with mysqldump before you drop a table on a real database.

IF EXISTS

DROP TABLE IF EXISTS temp_students;

This does not give an error when the table is not there. MySQL only shows a warning.


Drop more than one table

DROP TABLE IF EXISTS table_one, table_two;

Empty a table but keep it

TRUNCATE TABLE temp_students;

TRUNCATE removes all rows, but keeps the table. It is faster than DELETE without WHERE, and it restarts the AUTO_INCREMENT counter.

A table that is used by a FOREIGN KEY of another table cannot be dropped until that link is removed.


Summary

  • DROP TABLE name; removes the table and data.
  • Use IF EXISTS to avoid errors, and TRUNCATE to only empty it.
  • Next: ALTER TABLE.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.