SQL ALTER TABLE
ALTER TABLE changes the structure of an existing table without recreating it: add, rename or remove columns, or rename the table itself.
| Change | SQLite syntax |
|---|---|
| Add a column | ALTER TABLE t ADD COLUMN col type … |
| Rename a column | ALTER TABLE t RENAME COLUMN old TO new |
| Drop a column | ALTER TABLE t DROP COLUMN col (3.35+) |
| Rename the table | ALTER TABLE t RENAME TO new_name |
Adding a column
Customers get an optional phone number. Existing rows get NULL (or the column's default):
ALTER TABLE customers ADD COLUMN phone TEXT;
ALTER TABLE customers ADD COLUMN loyalty_points INTEGER NOT NULL DEFAULT 0;
UPDATE customers SET phone = '+49 30 5550101', loyalty_points = 120 WHERE customer_id = 1;
SELECT customer_id, name, phone, loyalty_points
FROM customers
WHERE customer_id <= 3
ORDER BY customer_id;1 row affected.
customer_id | name | phone | loyalty_points
------------+-----------------+----------------+---------------
1 | Corner Cafe | +49 30 5550101 | 120
2 | Bright Bytes | NULL | 0
3 | Nonna's Kitchen | NULL | 0
(3 rows)
A new NOT NULL column needs a DEFAULT, otherwise the existing rows would break the rule:
ALTER TABLE customers ADD COLUMN vat_number TEXT NOT NULL;Error: Cannot add a NOT NULL column with default value NULL
Renaming a column
ALTER TABLE products RENAME COLUMN stock TO units_in_stock;
SELECT name, units_in_stock FROM products WHERE product_id = 1;name | units_in_stock
---------------+---------------
Espresso Beans | 40
(1 row)
Dropping a column
ALTER TABLE suppliers DROP COLUMN contact;
SELECT name FROM pragma_table_info('suppliers');name
-----------
supplier_id
name
city
country
(4 rows)
SQLite refuses to drop a column that is a primary key, part of an index or a unique constraint, or referenced by a foreign key.
Renaming a table
ALTER TABLE categories RENAME TO product_categories;
SELECT name FROM sqlite_schema WHERE type = 'table' AND name LIKE '%categor%';name
------------------
product_categories
(1 row)
Differences between databases
SQLite's ALTER TABLE is deliberately small. Other databases can do more:
| Change | MySQL | PostgreSQL | SQL Server |
|---|---|---|---|
| Change a type | MODIFY COLUMN col type |
ALTER COLUMN col TYPE type |
ALTER COLUMN col type |
| Add a constraint | ADD CONSTRAINT … |
ADD CONSTRAINT … |
ADD CONSTRAINT … |
| Drop a constraint | DROP CONSTRAINT name |
DROP CONSTRAINT name |
DROP CONSTRAINT name |
| Rename column | RENAME COLUMN a TO b |
RENAME COLUMN a TO b |
EXEC sp_rename 't.a', 'b' |
To change a column type or add a constraint in SQLite, the standard recipe is: create a new table with the desired definition, copy the data with INSERT INTO new SELECT … FROM old, drop the old table and rename the new one — all inside a transaction.
Test yourself
What value do existing rows get in a newly added column without a default?
- 0
- An empty string
- NULL
- The value of the first row
Which statement renames a column in SQLite?
- ALTER TABLE t MODIFY a b
- ALTER TABLE t RENAME COLUMN a TO b
- RENAME COLUMN t.a TO b
- UPDATE t SET COLUMN a = b