DROP TABLE
Delete a whole table — its structure and all its data — with DROP TABLE, and use it safely.
At the end of the season you might have temporary tables you no longer need, like auction_draft. DROP TABLE removes a table from the database completely.
Definition
DROP TABLE is a DDL statement that permanently deletes a table from the database — its structure (columns, data types, constraints, indexes) AND all the rows stored in it.
- Permanently — there is no recycle bin. Unless you have a backup, it's gone.
- Structure AND data — after
DROP, the table doesn't exist anymore.SELECT * FROM playerswould fail with "Invalid object name 'players'."
Syntax
sql
DROP TABLE table_name;
-- Only if it exists (SQL Server 2016+, MySQL, PostgreSQL)
DROP TABLE IF EXISTS table_name;
-- Several tables at once
DROP TABLE table1, table2;| Part | Meaning |
|---|---|
DROP TABLE | "Delete this whole table" |
IF EXISTS | Don't raise an error if the table is already gone |
table_name | The table to remove |
Example — DROP TABLE players
Before: the players table exists with columns id, name, team, country, runs.
sql
DROP TABLE players;After: Table deleted — players no longer exists.
Step by step: 1️⃣ DROP TABLE → 2️⃣ players (gone!)
Warning: Deletes the table AND all its data. This cannot be undone!
Example — proving the table is gone
sql
CREATE TABLE auction_draft (
player VARCHAR(50),
price_cr DECIMAL(5,2)
);
DROP TABLE auction_draft;
SELECT * FROM auction_draft; -- ❌ Invalid object name 'auction_draft'DROP vs DELETE — do not mix them up
DELETE FROM players | DROP TABLE players | |
|---|---|---|
| Family | DML | DDL |
| Removes | Rows (data) only | The whole table |
| Table afterwards | Still exists (empty) | Does not exist |
| Can filter with WHERE? | Yes | No |
(Full comparison with TRUNCATE is in the DML chapter.)
When can DROP fail?
- The table doesn't exist → "Cannot drop the table 'x', because it does not exist or you do not have permission." Use
IF EXISTS. - Another table's FOREIGN KEY references it → SQL Server refuses. Drop the child table (or its foreign key) first.
- No permission — you need
ALTERpermission on the schema orCONTROLpermission on the table.
Common mistakes
- ❌ Running
DROP TABLEon the wrong database/server (e.g. production instead of test). ✅ Check which database you are connected to (USE ipl_db;) before running DDL. - ❌ Using
DROPwhen you only wanted to empty the table. ✅ UseDELETE(orTRUNCATE) to remove rows and keep the table. - ❌ Dropping a parent table that has child tables. ✅ Drop children first, or remove the foreign key.
- ❌ Writing
DROP players. ✅DROP TABLE players— the object type is required.
SQL Server vs MySQL / PostgreSQL
DROP TABLE IF EXISTS works in SQL Server 2016 and later, MySQL and PostgreSQL. On older SQL Server versions:
sql
IF OBJECT_ID('dbo.auction_draft', 'U') IS NOT NULL
DROP TABLE dbo.auction_draft;Interview tip
Q: Can you roll back a DROP TABLE?
A: In SQL Server, yes — but only if it was run inside an explicit transaction (BEGIN TRANSACTION … ROLLBACK) that hasn't been committed yet. Once committed (or in MySQL, where DDL auto-commits), the only way back is restoring a backup.
Key points to remember
- DROP TABLE permanently deletes a table: its structure AND all its data.
- After DROP, querying the table fails with 'Invalid object name'.
- DROP TABLE IF EXISTS avoids an error when the table is missing (SQL Server 2016+).
- DELETE removes rows but keeps the table; DROP removes the whole table.
- A table referenced by another table's FOREIGN KEY cannot be dropped until the child/foreign key is removed.
- Always double-check the database you're connected to before running DROP.
Try it yourself
Create, then drop a temporary table
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
DROP TABLE IF EXISTS — safe to run twice
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
DELETE vs DROP side by side
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: clean up after yourself
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
