DROP TABLE removes a table from the database: its columns, its rows and its rules all go.
DROP TABLE table_name;An example
Section titled “An example”To remove the UKProductions table made on SQL Create Table:
DROP TABLE UKProductions;Dropping a table that does not exist is an error. In setup scripts, DROP TABLE IF EXISTS UKProductions; skips the drop quietly instead; MySQL, PostgreSQL, SQLite and SQL Server all accept it.
Emptying a table and keeping it
Section titled “Emptying a table and keeping it”TRUNCATE TABLE removes every row but keeps the table, with its columns and rules, ready for new data:
TRUNCATE TABLE UKProductions;The result is the same as DELETE FROM without a WHERE clause, but TRUNCATE is much faster on a large table, because it releases the table’s storage rather than deleting rows one at a time. In exchange it is less flexible:
- It always removes every row; it takes no
WHEREclause. - In SQL Server and MySQL it restarts the automatic numbering of an ID column, described on SQL Auto Increment; PostgreSQL does so only when you add
RESTART IDENTITY. - Most databases refuse to truncate a table that another table’s foreign key points at.
SQLite has no TRUNCATE; DELETE FROM table_name; does the same job there.