Database designs change over time: a new feature needs an extra column, a name turns out to be unclear, an old table is no longer used. ALTER TABLE changes the structure of an existing table without losing its data, and DROP removes a table or other object entirely. This lesson also explains how DROP, DELETE and TRUNCATE differ, which is a common interview question.
In this lesson you will learn
- Adding a column with
ADD COLUMN - Renaming columns and tables
- Dropping columns, tables, views and indexes
IF EXISTSto avoid errors- DELETE vs TRUNCATE vs DROP
This lesson builds on CREATE TABLE and constraints.
Adding and Renaming
-- Add a column with a default value
ALTER TABLE students ADD COLUMN email TEXT DEFAULT 'pending';
SELECT id, name, email FROM students WHERE id <= 3;
-- Rename a column and a table
ALTER TABLE students RENAME COLUMN email TO contact_email;
ALTER TABLE courses RENAME TO subjects;
SELECT id, title FROM subjects LIMIT 2;
Output
+----+--------+---------+
| id | name | email |
+----+--------+---------+
| 1 | Nusrat | pending |
| 2 | Arif | pending |
| 3 | Tania | pending |
+----+--------+---------+
(3 rows)
+-----+-----------------+
| id | title |
+-----+-----------------+
| 101 | Data Structures |
| 102 | Databases |
+-----+-----------------+
(2 rows)
ALTER TABLE students ADD COLUMN email TEXT DEFAULT 'pending'adds a column. Existing rows receive the default; without a default they would get NULL.- You cannot add a
NOT NULLcolumn without a default to a table that already has rows, because the existing rows would break the rule. RENAME COLUMN old TO newandRENAME TO new_tablechange names while keeping the data. Queries, views and application code that use the old names must be updated too.
Other Changes
-- Remove a column (SQLite 3.35+, and all major databases)
ALTER TABLE students DROP COLUMN contact_email;
-- Change a column's type (syntax differs by database)
ALTER TABLE orders ALTER COLUMN amount TYPE NUMERIC(10,2); -- PostgreSQL
ALTER TABLE orders MODIFY amount DECIMAL(10,2); -- MySQL
ALTER TABLE orders ALTER COLUMN amount DECIMAL(10,2); -- SQL Server
-- Add a constraint to an existing table (not supported by SQLite)
ALTER TABLE orders ADD CONSTRAINT chk_amount CHECK (amount >= 0);
SQLite supports only add, rename and drop column. For other changes it uses a “rebuild” pattern: create a new table with the right design, copy the rows across, drop the old table and rename the new one, all inside a transaction.
DROP and DELETE
CREATE TABLE temp_log (id INTEGER PRIMARY KEY, note TEXT);
INSERT INTO temp_log (note) VALUES ('first'), ('second');
-- DELETE removes rows but keeps the table
DELETE FROM temp_log;
SELECT COUNT(*) AS rows_left FROM temp_log;
-- DROP removes the table itself
DROP TABLE temp_log;
SELECT COUNT(*) AS tables_named_temp_log FROM sqlite_master WHERE name = 'temp_log';
-- DROP ... IF EXISTS avoids an error when the table is missing
DROP TABLE IF EXISTS temp_log;
SELECT 'no error' AS result;
Output
+-----------+
| rows_left |
+-----------+
| 0 |
+-----------+
(1 row)
+-----------------------+
| tables_named_temp_log |
+-----------------------+
| 0 |
+-----------------------+
(1 row)
+----------+
| result |
+----------+
| no error |
+----------+
(1 row)
DELETE FROM temp_logremoves the rows; the empty table remains.DROP TABLE temp_logremoves the table, its data, indexes and triggers. The query againstsqlite_master(SQLite’s list of objects) confirms it is gone.DROP TABLE IF EXISTSdoes nothing if the table is missing, which makes setup scripts safe to run twice.- The same pattern works for other objects:
DROP VIEW,DROP INDEX.
DELETE vs TRUNCATE vs DROP
| DELETE | TRUNCATE | DROP | |
|---|---|---|---|
| Removes | Chosen rows (or all) | All rows | The whole table |
| WHERE allowed | Yes | No | No |
| Table structure kept | Yes | Yes | No |
| Speed on large tables | Slower (row by row) | Fast | Fast |
| Fires row triggers | Yes | No | No |
SQLite has no TRUNCATE; DELETE FROM table without WHERE is optimised to do the same job. Whether TRUNCATE can be rolled back depends on the database (yes in PostgreSQL and SQL Server, no in MySQL and Oracle).
Common Mistakes
- Dropping a table that other tables reference with foreign keys; drop or alter the dependants first.
- Renaming a column used by views or application code and breaking them.
- Running destructive changes on production without a backup or a tested migration script.
- Using DROP when DELETE was intended, losing the structure as well as the data.
Practice Exercises
Exercise 1. Add a column phone to students, then rename it to mobile.
Show answer
ALTER TABLE students ADD COLUMN phone TEXT;
ALTER TABLE students RENAME COLUMN phone TO mobile;
Exercise 2. Write a setup script line that creates a fresh audit table even if one already exists.
Show answer
DROP TABLE IF EXISTS audit;
CREATE TABLE audit (id INTEGER PRIMARY KEY, action TEXT, at TEXT);
Frequently Asked Questions
Does ALTER TABLE lock the table?
Often, yes, at least briefly. Some changes rewrite the whole table, which can take minutes on large tables. Many databases have online options, and teams usually run schema changes through migration tools.
Can I undo a DROP TABLE?
Only if it ran inside a transaction that you roll back (possible in PostgreSQL, SQL Server and SQLite) or if you restore from a backup. In MySQL, DROP commits immediately.
Summary
ALTER TABLEadds, renames and drops columns, and changes types and constraints.DROPremoves whole objects;IF EXISTSavoids errors.- DELETE removes rows, TRUNCATE empties a table, DROP removes the table.
- Plan schema changes carefully and keep backups.
