SQL Dropping Tables and a Small Design Project
What you will learn
- How to remove a table with
DROP TABLE, and in which order - Why a foreign key can block a drop
- How to build a small database from a description
- How to check that the design works
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). The tables you create exist only in your browser's copy of the data; Reset data removes them. Run the cells from top to bottom.
1. DROP TABLE
DROP TABLE name removes a table and all its rows. There is no "are you sure?" and no WHERE. IF EXISTS avoids an error when the table is not there:
CREATE TABLE lib_scratch (n INTEGER);
INSERT INTO lib_scratch VALUES (1), (2), (3);
SELECT COUNT(*) AS rows_before FROM lib_scratch;
rows_before
-----------
3
(1 row)
DROP TABLE lib_scratch;
SELECT COUNT(*) AS tables_named_scratch FROM sqlite_master WHERE name = 'lib_scratch';
tables_named_scratch
--------------------
0
(1 row)
DROP TABLE IF EXISTS lib_scratch;
(done)
CREATE TABLE lib_scratch (n INTEGER);
INSERT INTO lib_scratch VALUES (1), (2), (3);
SELECT COUNT(*) AS rows_before FROM lib_scratch;
rows_before
-----------
3
(1 row)
DROP TABLE lib_scratch;
SELECT COUNT(*) AS tables_named_scratch FROM sqlite_master WHERE name = 'lib_scratch';
tables_named_scratch
--------------------
0
(1 row)
DROP TABLE IF EXISTS lib_scratch;
(done)
CREATE TABLE lib_scratch (n INTEGER);
INSERT INTO lib_scratch VALUES (1), (2), (3);
SELECT COUNT(*) AS rows_before FROM lib_scratch;
rows_before
-----------
3
(1 row)
DROP TABLE lib_scratch;
SELECT COUNT(*) AS tables_named_scratch FROM sqlite_master WHERE name = 'lib_scratch';
tables_named_scratch
--------------------
0
(1 row)
DROP TABLE IF EXISTS lib_scratch;
(done)
The second statement removed the table, the third did nothing and gave no error. Dropping is permanent: only a backup brings the data back (lesson 5.4).
2. The order of dropping
A table that other tables point to with a foreign key should not be dropped while they exist. Drop the children first, then the parents; create in the opposite order:
| Action | Order |
|---|---|
| create | parent first, then the child |
| drop | child first, then the parent |
3. A small design project
Now put it all together. The description:
A small library lends books. Each book has a title, an author, and a year (maybe unknown), and its code must be unique. Each member has a name and an email that must be unique. A loan records which member borrowed which book, the date it started, and the date it was returned (empty while the book is out). A loan cannot end before it starts. A member cannot borrow the same book twice on the same day.
Build the three tables, parents first. The rules come straight from the description:
CREATE TABLE lib2_books (
book_id INTEGER PRIMARY KEY,
code TEXT NOT NULL UNIQUE,
title TEXT NOT NULL,
author TEXT NOT NULL,
year INTEGER
);
CREATE TABLE lib2_members (
member_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
);
CREATE TABLE lib2_loans (
loan_id INTEGER PRIMARY KEY,
book_id INTEGER NOT NULL REFERENCES lib2_books (book_id),
member_id INTEGER NOT NULL REFERENCES lib2_members (member_id),
loaned_on TEXT NOT NULL,
returned_on TEXT,
CHECK (returned_on IS NULL OR returned_on >= loaned_on),
UNIQUE (book_id, member_id, loaned_on)
);
SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'lib2_%' ORDER BY name;
name
------------
lib2_books
lib2_loans
lib2_members
(3 rows)
CREATE TABLE lib2_books (
book_id INTEGER PRIMARY KEY,
code TEXT NOT NULL UNIQUE,
title TEXT NOT NULL,
author TEXT NOT NULL,
year INTEGER
);
CREATE TABLE lib2_members (
member_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
);
CREATE TABLE lib2_loans (
loan_id INTEGER PRIMARY KEY,
book_id INTEGER NOT NULL REFERENCES lib2_books (book_id),
member_id INTEGER NOT NULL REFERENCES lib2_members (member_id),
loaned_on TEXT NOT NULL,
returned_on TEXT,
CHECK (returned_on IS NULL OR returned_on >= loaned_on),
UNIQUE (book_id, member_id, loaned_on)
);
SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'lib2_%' ORDER BY name;
name
------------
lib2_books
lib2_loans
lib2_members
(3 rows)
CREATE TABLE lib2_books (
book_id INTEGER PRIMARY KEY,
code TEXT NOT NULL UNIQUE,
title TEXT NOT NULL,
author TEXT NOT NULL,
year INTEGER
);
CREATE TABLE lib2_members (
member_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
);
CREATE TABLE lib2_loans (
loan_id INTEGER PRIMARY KEY,
book_id INTEGER NOT NULL REFERENCES lib2_books (book_id),
member_id INTEGER NOT NULL REFERENCES lib2_members (member_id),
loaned_on TEXT NOT NULL,
returned_on TEXT,
CHECK (returned_on IS NULL OR returned_on >= loaned_on),
UNIQUE (book_id, member_id, loaned_on)
);
SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'lib2_%' ORDER BY name;
name
------------
lib2_books
lib2_loans
lib2_members
(3 rows)
Fill them with a few made-up rows:
INSERT INTO lib2_books (code, title, author, year) VALUES
('B-001', 'Rain Songs', 'A. Karim', 2011),
('B-002', 'River Maps', 'N. Das', 2018),
('B-003', 'Night Train', 'A. Karim', NULL);
INSERT INTO lib2_members (name, email) VALUES ('Rina', 'rina@example.com'), ('Sam', 'sam@example.com');
INSERT INTO lib2_loans (book_id, member_id, loaned_on, returned_on) VALUES
(1, 1, '2026-09-01', '2026-09-10'), (2, 1, '2026-09-12', NULL), (1, 2, '2026-09-15', NULL);
SELECT (SELECT COUNT(*) FROM lib2_books) AS books, (SELECT COUNT(*) FROM lib2_members) AS members, (SELECT COUNT(*) FROM lib2_loans) AS loans;
books members loans
----- ------- -----
3 2 3
(1 row)
INSERT INTO lib2_books (code, title, author, year) VALUES
('B-001', 'Rain Songs', 'A. Karim', 2011),
('B-002', 'River Maps', 'N. Das', 2018),
('B-003', 'Night Train', 'A. Karim', NULL);
INSERT INTO lib2_members (name, email) VALUES ('Rina', 'rina@example.com'), ('Sam', 'sam@example.com');
INSERT INTO lib2_loans (book_id, member_id, loaned_on, returned_on) VALUES
(1, 1, '2026-09-01', '2026-09-10'), (2, 1, '2026-09-12', NULL), (1, 2, '2026-09-15', NULL);
SELECT (SELECT COUNT(*) FROM lib2_books) AS books, (SELECT COUNT(*) FROM lib2_members) AS members, (SELECT COUNT(*) FROM lib2_loans) AS loans;
books members loans
----- ------- -----
3 2 3
(1 row)
INSERT INTO lib2_books (code, title, author, year) VALUES
('B-001', 'Rain Songs', 'A. Karim', 2011),
('B-002', 'River Maps', 'N. Das', 2018),
('B-003', 'Night Train', 'A. Karim', NULL);
INSERT INTO lib2_members (name, email) VALUES ('Rina', 'rina@example.com'), ('Sam', 'sam@example.com');
INSERT INTO lib2_loans (book_id, member_id, loaned_on, returned_on) VALUES
(1, 1, '2026-09-01', '2026-09-10'), (2, 1, '2026-09-12', NULL), (1, 2, '2026-09-15', NULL);
SELECT (SELECT COUNT(*) FROM lib2_books) AS books, (SELECT COUNT(*) FROM lib2_members) AS members, (SELECT COUNT(*) FROM lib2_loans) AS loans;
books members loans
----- ------- -----
3 2 3
(1 row)
4. Ask the library's questions
A design is good when the questions can be answered. Who has which book right now, and how often was each book borrowed?
SELECT m.name, b.title, l.loaned_on
FROM lib2_loans AS l
JOIN lib2_members AS m ON m.member_id = l.member_id
JOIN lib2_books AS b ON b.book_id = l.book_id
WHERE l.returned_on IS NULL
ORDER BY l.loaned_on;
name title loaned_on
---- ---------- ----------
Rina River Maps 2026-09-12
Sam Rain Songs 2026-09-15
(2 rows)
SELECT b.title, COUNT(l.loan_id) AS times_borrowed
FROM lib2_books AS b
LEFT JOIN lib2_loans AS l ON l.book_id = b.book_id
GROUP BY b.book_id, b.title
ORDER BY times_borrowed DESC, b.title;
title times_borrowed
----------- --------------
Rain Songs 2
River Maps 1
Night Train 0
(3 rows)
SELECT m.name, b.title, l.loaned_on
FROM lib2_loans AS l
JOIN lib2_members AS m ON m.member_id = l.member_id
JOIN lib2_books AS b ON b.book_id = l.book_id
WHERE l.returned_on IS NULL
ORDER BY l.loaned_on;
name title loaned_on
---- ---------- ----------
Rina River Maps 2026-09-12
Sam Rain Songs 2026-09-15
(2 rows)
SELECT b.title, COUNT(l.loan_id) AS times_borrowed
FROM lib2_books AS b
LEFT JOIN lib2_loans AS l ON l.book_id = b.book_id
GROUP BY b.book_id, b.title
ORDER BY times_borrowed DESC, b.title;
title times_borrowed
----------- --------------
Rain Songs 2
River Maps 1
Night Train 0
(3 rows)
SELECT m.name, b.title, l.loaned_on
FROM lib2_loans AS l
JOIN lib2_members AS m ON m.member_id = l.member_id
JOIN lib2_books AS b ON b.book_id = l.book_id
WHERE l.returned_on IS NULL
ORDER BY l.loaned_on;
name title loaned_on
---- ---------- ----------
Rina River Maps 2026-09-12
Sam Rain Songs 2026-09-15
(2 rows)
SELECT b.title, COUNT(l.loan_id) AS times_borrowed
FROM lib2_books AS b
LEFT JOIN lib2_loans AS l ON l.book_id = b.book_id
GROUP BY b.book_id, b.title
ORDER BY times_borrowed DESC, b.title;
title times_borrowed
----------- --------------
Rain Songs 2
River Maps 1
Night Train 0
(3 rows)
5. Try to break the rules
Test each rule of the description. Every one of these must be refused:
A second book with the same code (UNIQUE):
INSERT INTO lib2_books (code, title, author) VALUES ('B-001', 'Copy', 'Nobody');
IntegrityError: UNIQUE constraint failed: lib2_books.code
A loan for a book that does not exist (foreign key):
INSERT INTO lib2_loans (book_id, member_id, loaned_on) VALUES (999, 1, '2026-10-01');
IntegrityError: FOREIGN KEY constraint failed
A loan that ends before it starts (CHECK):
INSERT INTO lib2_loans (book_id, member_id, loaned_on, returned_on) VALUES (3, 2, '2026-10-05', '2026-10-01');
IntegrityError: CHECK constraint failed: returned_on IS NULL OR returned_on >= loaned_on
The same member borrowing the same book on the same day (UNIQUE):
INSERT INTO lib2_loans (book_id, member_id, loaned_on) VALUES (1, 2, '2026-09-15');
IntegrityError: UNIQUE constraint failed: lib2_loans.book_id, lib2_loans.member_id, lib2_loans.loaned_on
A second book with the same code (UNIQUE):
INSERT INTO lib2_books (code, title, author) VALUES ('B-001', 'Copy', 'Nobody');
IntegrityError: UNIQUE constraint failed: lib2_books.code
A loan for a book that does not exist (foreign key):
INSERT INTO lib2_loans (book_id, member_id, loaned_on) VALUES (999, 1, '2026-10-01');
IntegrityError: FOREIGN KEY constraint failed
A loan that ends before it starts (CHECK):
INSERT INTO lib2_loans (book_id, member_id, loaned_on, returned_on) VALUES (3, 2, '2026-10-05', '2026-10-01');
IntegrityError: CHECK constraint failed: returned_on IS NULL OR returned_on >= loaned_on
The same member borrowing the same book on the same day (UNIQUE):
INSERT INTO lib2_loans (book_id, member_id, loaned_on) VALUES (1, 2, '2026-09-15');
IntegrityError: UNIQUE constraint failed: lib2_loans.book_id, lib2_loans.member_id, lib2_loans.loaned_on
A second book with the same code (UNIQUE):
INSERT INTO lib2_books (code, title, author) VALUES ('B-001', 'Copy', 'Nobody');
IntegrityError: UNIQUE constraint failed: lib2_books.code
A loan for a book that does not exist (foreign key):
INSERT INTO lib2_loans (book_id, member_id, loaned_on) VALUES (999, 1, '2026-10-01');
IntegrityError: FOREIGN KEY constraint failed
A loan that ends before it starts (CHECK):
INSERT INTO lib2_loans (book_id, member_id, loaned_on, returned_on) VALUES (3, 2, '2026-10-05', '2026-10-01');
IntegrityError: CHECK constraint failed: returned_on IS NULL OR returned_on >= loaned_on
The same member borrowing the same book on the same day (UNIQUE):
INSERT INTO lib2_loans (book_id, member_id, loaned_on) VALUES (1, 2, '2026-09-15');
IntegrityError: UNIQUE constraint failed: lib2_loans.book_id, lib2_loans.member_id, lib2_loans.loaned_on
6. Clean up in the right order
Drop the child first, then the parents:
DROP TABLE lib2_loans;
DROP TABLE lib2_members;
DROP TABLE lib2_books;
SELECT COUNT(*) AS lib2_tables_left FROM sqlite_master WHERE name LIKE 'lib2_%';
lib2_tables_left
----------------
0
(1 row)
DROP TABLE lib2_loans;
DROP TABLE lib2_members;
DROP TABLE lib2_books;
SELECT COUNT(*) AS lib2_tables_left FROM sqlite_master WHERE name LIKE 'lib2_%';
lib2_tables_left
----------------
0
(1 row)
DROP TABLE lib2_loans;
DROP TABLE lib2_members;
DROP TABLE lib2_books;
SELECT COUNT(*) AS lib2_tables_left FROM sqlite_master WHERE name LIKE 'lib2_%';
lib2_tables_left
----------------
0
(1 row)
Worked example: from a description to a design
| Phrase in the description | Becomes |
|---|---|
| "each book has a title..." | a table books with title NOT NULL |
| "its code must be unique" | code TEXT NOT NULL UNIQUE |
| "which member borrowed which book" | a loans table with two foreign keys |
| "empty while the book is out" | returned_on allowed to be NULL |
| "cannot end before it starts" | CHECK (returned_on IS NULL OR returned_on >= loaned_on) |
| "not the same book twice on the same day" | UNIQUE (book_id, member_id, loaned_on) |
Read the description sentence by sentence and turn each fact into a column or a rule. The library is made up.
Common mistakes
- Dropping a parent before its children.
DROP TABLEon the wrong database.- No backup before dropping.
- A design with no test: rules that were never tried.
- Putting a repeated group in one column instead of a child table.
Key takeaways
DROP TABLEremoves the table and its rows for good;IF EXISTSavoids the error.- Create parents before children; drop children before parents.
- Turn a description into tables and rules sentence by sentence.
- Fill the tables, ask the questions the design must answer, and try to break the rules.
Practice
1. Which statement removes a table only if it exists?
Show answer
DROP TABLE IF EXISTS name;
2. In which order do you drop members, books and loans (loans point to the others)?
Show answer
loans first, then members and books.
3. How do you express "a loan cannot end before it starts"?
Show answer
CHECK (returned_on IS NULL OR returned_on >= loaned_on).
4. Why test a new design with questions?
Show answer
If the questions cannot be answered with plain queries, the design is missing something.
5. What happens to the rows of a dropped table?
Show answer
They are gone with it; only a backup can bring them back.
6. Which rule makes a book code unique?
Show answer
code TEXT NOT NULL UNIQUE.