SQL DROP TABLE
Deletes a table together with its data, indexes and triggers. The differences worth knowing: which engines take IF EXISTS, what happens to foreign keys and views, and where Oracle parks the table first.
- Категория
- Таблицы
- In this category
- 6 of 16
Синтаксис
DROP TABLE [IF EXISTS] table_name
DROP TABLE table_name CASCADE -- PostgreSQL: drops dependent views and constraints too
DROP TABLE table_name PURGE -- Oracle: skip the recycle bin
Each block is a separate form. The support table and the examples show which engine takes which.
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Да | IF EXISTS from SQL Server 2016 |
| PostgreSQL | Да | RESTRICT is the default; CASCADE takes dependent objects with it |
| MySQL | Да | several tables in one statement; FOREIGN_KEY_CHECKS = 0 overrides refusals |
| SQLite | Да | IF EXISTS; dropping a table drops its indexes and triggers with it |
| Oracle | Да | lands in the recycle bin first; PURGE drops for real; IF EXISTS from 23c |
Примеры
Any engine
DROP TABLE IF EXISTS stage_orders;
PostgreSQL
DROP TABLE orders_archive CASCADE; -- dependent views go too
Oracle
DROP TABLE orders_archive PURGE; -- do not park it in the recycle bin
MySQL
DROP TABLE IF EXISTS tmp_a, tmp_b, tmp_c;
Полезно знать
- SQL Server refuses to drop a table that a foreign key references: drop the constraint or the referencing table first. PostgreSQL's CASCADE does it for you, dropping dependent views and the referencing foreign key constraints (the referencing tables stay) with nothing more than a NOTICE listing them, which is exactly why it should be typed deliberately.
- Oracle does not free the space immediately: a dropped table sits in the recycle bin as a BIN$ name, still counting against your quota, until PURGE or space pressure removes it. FLASHBACK TABLE ... TO BEFORE DROP can bring it back; DROP TABLE ... PURGE skips the bin.
- IF EXISTS arrived in SQL Server 2016 and Oracle 23c; PostgreSQL, MySQL and SQLite have had it for years. On anything older, query the catalog first or catch the error.
- MySQL's DROP TABLE takes a list and, with IF EXISTS, reports a warning rather than an error for names that do not exist, which makes cleanup scripts simple: DROP TABLE IF EXISTS a, b, c.
Questions about DROP TABLE
What is the difference between DROP TABLE and TRUNCATE TABLE?
TRUNCATE empties the table and keeps the definition, indexes and permissions. DROP removes the whole object; you get it back only by recreating it, or on Oracle with flashback drop while the recycle bin still holds it. Truncate to reload, drop to remove.
Can I undo a DROP TABLE?
Inside an open transaction on SQL Server, PostgreSQL or SQLite, ROLLBACK undoes it, because DDL is transactional there. MySQL and Oracle commit DDL implicitly, so the recovery paths are backups or, on Oracle, FLASHBACK TABLE ... TO BEFORE DROP while the recycle bin still holds the table.
Why does DROP TABLE fail with a foreign key error?
Another table's foreign key points at this one. Drop the constraint or the referencing table first; on PostgreSQL, DROP TABLE ... CASCADE does it in one statement, and on MySQL, FOREIGN_KEY_CHECKS = 0 for the session is the blunt-instrument version.
Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.
DDL that matches the engine you're connected to
SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.