SQLite ALTER TABLE Statement
The ALTER TABLE Statement
ALTER TABLE changes an existing table. In SQLite, it can do four things:
- Add a column
- Rename a column
- Delete a column
- Rename the table
SQLite cannot change the data type or the constraints of an existing column. You will see how to do that at the end of this page.
Add a Column
ALTER TABLE table_name
ADD COLUMN column_name datatype;
Example
Add an Email column to the Customers table:
ALTER TABLE Customers
ADD COLUMN Email TEXT;
Try it Yourself »
The new column is NULL in all the existing records, unless you give it a DEFAULT value.
Rename a Column
ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;
Note: RENAME COLUMN works in SQLite 3.25.0 and later.
Delete a Column
ALTER TABLE table_name
DROP COLUMN column_name;
Note: DROP COLUMN works in SQLite 3.35.0 and later. You cannot delete a column that is a primary key, or that has a UNIQUE constraint.
Rename a Table
ALTER TABLE table_name
RENAME TO new_table_name;
Change a Column
To change the data type or the constraints of a column, make a new table, copy the data, delete the old table, and give the new table the old name:
Example
CREATE TABLE NewShippers (
ShipperID INTEGER PRIMARY KEY,
ShipperName TEXT NOT NULL,
Phone TEXT
);
INSERT INTO NewShippers
SELECT * FROM Shippers;
DROP TABLE Shippers;
ALTER TABLE NewShippers RENAME TO Shippers;
Try it Yourself »
Tip: Put these statements in a transaction, so that nothing is changed if one of them fails.