SQL ALTER TABLE
What you will learn
- How to add a column to a table that has rows
- How to rename a column and a table
- How to drop a column, and when it fails
- The safe way to make a bigger change
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). The changes happen only in your browser's copy of the data; Reset data puts everything back. Run the cells from top to bottom.
1. A table that changes
A database is rarely finished. New features need new columns, names are fixed, old columns are retired. ALTER TABLE changes the structure of a table without losing its rows. In a live system a structure change must be planned (Database Fundamentals, lesson 8.7). We practise on our own table:
CREATE TABLE lib_customers (customer_id INTEGER PRIMARY KEY, full_name TEXT NOT NULL, email TEXT);
INSERT INTO lib_customers (full_name, email) VALUES ('Rina Akter', 'rina@example.com'), ('Sam Roy', 'sam@example.com');
SELECT * FROM lib_customers ORDER BY customer_id;
customer_id full_name email
----------- ---------- ----------------
1 Rina Akter rina@example.com
2 Sam Roy sam@example.com
(2 rows)
CREATE TABLE lib_customers (customer_id INTEGER PRIMARY KEY, full_name TEXT NOT NULL, email TEXT);
INSERT INTO lib_customers (full_name, email) VALUES ('Rina Akter', 'rina@example.com'), ('Sam Roy', 'sam@example.com');
SELECT * FROM lib_customers ORDER BY customer_id;
customer_id full_name email
----------- ---------- ----------------
1 Rina Akter rina@example.com
2 Sam Roy sam@example.com
(2 rows)
CREATE TABLE lib_customers (customer_id INTEGER PRIMARY KEY, full_name TEXT NOT NULL, email TEXT);
INSERT INTO lib_customers (full_name, email) VALUES ('Rina Akter', 'rina@example.com'), ('Sam Roy', 'sam@example.com');
SELECT * FROM lib_customers ORDER BY customer_id;
customer_id full_name email
----------- ---------- ----------------
1 Rina Akter rina@example.com
2 Sam Roy sam@example.com
(2 rows)
2. Add a column
ALTER TABLE ... ADD COLUMN adds a column. The existing rows get NULL in it (or the default):
ALTER TABLE lib_customers ADD COLUMN phone TEXT;
SELECT * FROM lib_customers ORDER BY customer_id;
customer_id full_name email phone
----------- ---------- ---------------- -----
1 Rina Akter rina@example.com NULL
2 Sam Roy sam@example.com NULL
(2 rows)
ALTER TABLE lib_customers ADD COLUMN phone TEXT;
SELECT * FROM lib_customers ORDER BY customer_id;
customer_id full_name email phone
----------- ---------- ---------------- -----
1 Rina Akter rina@example.com NULL
2 Sam Roy sam@example.com NULL
(2 rows)
ALTER TABLE lib_customers ADD COLUMN phone TEXT;
SELECT * FROM lib_customers ORDER BY customer_id;
customer_id full_name email phone
----------- ---------- ---------------- -----
1 Rina Akter rina@example.com NULL
2 Sam Roy sam@example.com NULL
(2 rows)
A new column that is NOT NULL needs a default, because the old rows must get some value:
ALTER TABLE lib_customers ADD COLUMN city TEXT NOT NULL;
OperationalError: Cannot add a NOT NULL column with default value NULL
ALTER TABLE lib_customers ADD COLUMN city TEXT NOT NULL DEFAULT 'unknown';
SELECT full_name, city FROM lib_customers ORDER BY customer_id;
full_name city
---------- -------
Rina Akter unknown
Sam Roy unknown
(2 rows)
ALTER TABLE lib_customers ADD COLUMN city TEXT NOT NULL;
OperationalError: Cannot add a NOT NULL column with default value NULL
ALTER TABLE lib_customers ADD COLUMN city TEXT NOT NULL DEFAULT 'unknown';
SELECT full_name, city FROM lib_customers ORDER BY customer_id;
full_name city
---------- -------
Rina Akter unknown
Sam Roy unknown
(2 rows)
ALTER TABLE lib_customers ADD COLUMN city TEXT NOT NULL;
OperationalError: Cannot add a NOT NULL column with default value NULL
ALTER TABLE lib_customers ADD COLUMN city TEXT NOT NULL DEFAULT 'unknown';
SELECT full_name, city FROM lib_customers ORDER BY customer_id;
full_name city
---------- -------
Rina Akter unknown
Sam Roy unknown
(2 rows)
According to the SQLite documentation, a column added this way cannot be a PRIMARY KEY or UNIQUE, and a NOT NULL column needs a default other than NULL.
3. Rename a column or a table
ALTER TABLE lib_customers RENAME COLUMN full_name TO name;
SELECT * FROM lib_customers ORDER BY customer_id;
customer_id name email phone city
----------- ---------- ---------------- ----- -------
1 Rina Akter rina@example.com NULL unknown
2 Sam Roy sam@example.com NULL unknown
(2 rows)
ALTER TABLE lib_customers RENAME TO lib_clients;
SELECT COUNT(*) AS rows_in_renamed_table FROM lib_clients;
rows_in_renamed_table
---------------------
2
(1 row)
ALTER TABLE lib_customers RENAME COLUMN full_name TO name;
SELECT * FROM lib_customers ORDER BY customer_id;
customer_id name email phone city
----------- ---------- ---------------- ----- -------
1 Rina Akter rina@example.com NULL unknown
2 Sam Roy sam@example.com NULL unknown
(2 rows)
ALTER TABLE lib_customers RENAME TO lib_clients;
SELECT COUNT(*) AS rows_in_renamed_table FROM lib_clients;
rows_in_renamed_table
---------------------
2
(1 row)
ALTER TABLE lib_customers RENAME COLUMN full_name TO name;
SELECT * FROM lib_customers ORDER BY customer_id;
customer_id name email phone city
----------- ---------- ---------------- ----- -------
1 Rina Akter rina@example.com NULL unknown
2 Sam Roy sam@example.com NULL unknown
(2 rows)
ALTER TABLE lib_customers RENAME TO lib_clients;
SELECT COUNT(*) AS rows_in_renamed_table FROM lib_clients;
rows_in_renamed_table
---------------------
2
(1 row)
Renaming is easy for the database but breaks every query that used the old name. Check the programs and views that refer to it first.
4. Drop a column
ALTER TABLE ... DROP COLUMN removes a column and its data:
ALTER TABLE lib_clients DROP COLUMN phone;
SELECT name, email, city FROM lib_clients ORDER BY customer_id;
name email city
---------- ---------------- -------
Rina Akter rina@example.com unknown
Sam Roy sam@example.com unknown
(2 rows)
ALTER TABLE lib_clients DROP COLUMN phone;
SELECT name, email, city FROM lib_clients ORDER BY customer_id;
name email city
---------- ---------------- -------
Rina Akter rina@example.com unknown
Sam Roy sam@example.com unknown
(2 rows)
ALTER TABLE lib_clients DROP COLUMN phone;
SELECT name, email, city FROM lib_clients ORDER BY customer_id;
name email city
---------- ---------------- -------
Rina Akter rina@example.com unknown
Sam Roy sam@example.com unknown
(2 rows)
SQLite refuses to drop a column that is part of the primary key, has a UNIQUE rule, is indexed, or is used in a CHECK, a foreign key, a view or a trigger (according to its documentation). A dropped column cannot be brought back, so back up first (lesson 5.4).
5. What SQLite cannot alter
SQLite's ALTER TABLE is limited. You cannot, in the SQLite version of the browser, change the type of a column, or add a constraint (a CHECK, a foreign key, a primary key) to an existing column. The documentation says newer SQLite versions add some operations; check the version you use. Other databases let you change more.
6. The safe way to make a bigger change
When ALTER TABLE cannot do it, build a new table with the right design, copy the data, and swap the names, all in a transaction (lesson 5.5):
BEGIN;
CREATE TABLE lib_clients_new (
customer_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
city TEXT NOT NULL DEFAULT 'unknown'
);
INSERT INTO lib_clients_new (customer_id, name, email, city)
SELECT customer_id, name, email, city FROM lib_clients;
DROP TABLE lib_clients;
ALTER TABLE lib_clients_new RENAME TO lib_clients;
COMMIT;
SELECT * FROM lib_clients ORDER BY customer_id;
customer_id name email city
----------- ---------- ---------------- -------
1 Rina Akter rina@example.com unknown
2 Sam Roy sam@example.com unknown
(2 rows)
BEGIN;
CREATE TABLE lib_clients_new (
customer_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
city TEXT NOT NULL DEFAULT 'unknown'
);
INSERT INTO lib_clients_new (customer_id, name, email, city)
SELECT customer_id, name, email, city FROM lib_clients;
DROP TABLE lib_clients;
ALTER TABLE lib_clients_new RENAME TO lib_clients;
COMMIT;
SELECT * FROM lib_clients ORDER BY customer_id;
customer_id name email city
----------- ---------- ---------------- -------
1 Rina Akter rina@example.com unknown
2 Sam Roy sam@example.com unknown
(2 rows)
BEGIN;
CREATE TABLE lib_clients_new (
customer_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
city TEXT NOT NULL DEFAULT 'unknown'
);
INSERT INTO lib_clients_new (customer_id, name, email, city)
SELECT customer_id, name, email, city FROM lib_clients;
DROP TABLE lib_clients;
ALTER TABLE lib_clients_new RENAME TO lib_clients;
COMMIT;
SELECT * FROM lib_clients ORDER BY customer_id;
customer_id name email city
----------- ---------- ---------------- -------
1 Rina Akter rina@example.com unknown
2 Sam Roy sam@example.com unknown
(2 rows)
The table now has a NOT NULL UNIQUE email, a rule that ALTER TABLE could not add. Check the copy before you drop the old table.
In other databases. Many databases can change a column's type or add a constraint with
ALTER TABLEdirectly, with their own syntax and with different effects on large tables. Check the documentation of the database you use.
Worked example: rename a column safely
You want to rename phone to mobile in a live system.
| Step | Do |
|---|---|
| 1 | add the new column: ALTER TABLE ... ADD COLUMN mobile TEXT; |
| 2 | copy the data: UPDATE ... SET mobile = phone; |
| 3 | change the programs to use mobile |
| 4 | after a safe period, drop the old column |
Four small, reversible steps (until the last) instead of one risky rename. The table is made up (Database Fundamentals, lesson 8.7 does the same).
Common mistakes
- Dropping a column without a backup.
- Renaming without updating the queries that use the name.
- Adding a
NOT NULLcolumn without a default. - Trying to add a constraint to a column with
ALTER TABLEin SQLite. - Altering a big live table at a busy time.
Key takeaways
ADD COLUMN,RENAME COLUMN,RENAME TOandDROP COLUMNchange a table without losing its rows.- A new
NOT NULLcolumn needs a default. - SQLite cannot change a column's type or add a constraint with
ALTER TABLE. - For bigger changes: new table, copy, drop, rename, all in a transaction.
Practice
1. Write the statement that adds a column phone to customers.
Show answer
ALTER TABLE customers ADD COLUMN phone TEXT;
2. What do the existing rows hold in the new column?
Show answer
NULL, or the column's default if it has one.
3. Why does adding a NOT NULL column without a default fail?
Show answer
The existing rows would have no value for it, which NOT NULL forbids.
4. What can go wrong when you rename a column?
Show answer
Every query, view or program that uses the old name stops working.
5. How do you add a UNIQUE rule to an existing SQLite column?
Show answer
Build a new table with the rule, copy the data, drop the old table and rename the new one.
6. Why do the table swap inside a transaction?
Show answer
If any step fails you can roll back, so you never end up with half the change.