DROP TABLE permanently removes one or more tables and their data from a MySQL database.
Use DROP TABLE when a table is no longer needed, you want to reclaim storage, or you need to recreate the table with a new structure.
MySQL uses DROP TABLE [IF EXISTS] table_name;
. The IF EXISTS
clause prevents errors if the table is missing.
Separate table names with commas: DROP TABLE IF EXISTS Orders, OrderItems;
.MySQL drops each listed table in the same statement.
Use DROP TABLE ... WAIT n
to wait n seconds for locks to clear, or DROP TABLE ... NOWAIT
(MySQL 8.0.23+) to fail immediately.
The TEMPORARY
keyword drops only temporary tables in the current session. It has no effect on permanent tables.
Yes. Referencing foreign keys become invalid automatically.Check dependent tables before dropping.
The shop no longer tracks flash-sale orders. Remove the table safely:
DROP TABLE IF EXISTS FlashSaleOrders;
Run CREATE TABLE backup_Orders LIKE Orders;
then INSERT INTO backup_Orders SELECT * FROM Orders;
. After confirming, execute DROP TABLE Orders;
.
1) Use IF EXISTS
to avoid runtime errors. 2) Back up critical data first.3) Run in transactions when supported by the storage engine.
See below for frequent pitfalls and fixes.
.
Yes, InnoDB releases space inside the tablespace. To shrink the tablespace file, run OPTIMIZE TABLE
or enable innodb_file_per_table.
Not directly. Restore from a backup or point-in-time recovery if binary logging is enabled.
With InnoDB, the metadata change commits immediately and cannot be rolled back, even if wrapped in a transaction block.