SQL Drop Table
DROP TABLE removes a table and all its rows. The table is gone, and later statements can no longer use it.
A scratch table with one row
The scratch table holds one draft row. The SELECT proves the table and its row exist.
CREATE TABLE scratch (
note TEXT
);
INSERT INTO scratch VALUES ('Draft row');
SELECT note FROM scratch;Drop the table
DROP TABLE removes the scratch table. The statement returns no rows.
CREATE TABLE scratch (
note TEXT
);
DROP TABLE scratch;Example
CREATE TABLE scratch (
note TEXT
);
INSERT INTO scratch VALUES ('Draft row');
DROP TABLE scratch;
SELECT note FROM scratch;Key points
- DROP TABLE: the statement that removes a table
- Dropping a table removes its rows with it.
- DROP TABLE IF EXISTS drops a table and stays quiet when the table is already gone.
- A SELECT from a dropped table fails with an error.
In this example, the code builds a scratch table and adds one row. DROP TABLE then removes the table. The last SELECT asks the dropped table for rows, and SQLite answers with an error.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท DB Fiddle
Exercise
What happens to a table's rows when the table is dropped?