SQL ALTER TABLE: add, rename and drop columns
Tables rarely stay the way they were built. ALTER TABLE lets you add a column for email addresses, give a column a clearer name or remove one nobody fills in anymore, all without losing the data that is already there.
In short: ALTER TABLE customers ADD COLUMN email TEXT adds a column, RENAME COLUMN old TO new renames one, DROP COLUMN phone removes one and RENAME TO new_name renames the whole table. In SQLite, each ALTER TABLE statement makes exactly one of these changes.
What you should know first: creating tables with CREATE TABLE and changing data with UPDATE.
Try it now
After your change, the exercise shows the whole table. Keep an eye on the column names in the header row, because they're part of the check.
Task Customer service wants to store email addresses from now on. Add the column email (text, no default) to the customers table. Existing customers keep all their data.
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
How ALTER TABLE works
ALTER TABLE is followed by the table you want to change and then the change itself. SQLite, the database behind these exercises, supports four of them:
| Change | Statement |
|---|---|
| add a column | ALTER TABLE customers ADD COLUMN email TEXT; |
| rename a column | ALTER TABLE employees RENAME COLUMN salary TO annual_salary; |
| rename the table | ALTER TABLE order_items RENAME TO order_lines; |
| drop a column | ALTER TABLE customers DROP COLUMN phone; |
Here's one from HR: for the phone directory, everyone gets an extension number. The column is new, and so far only Sarah Long's extension has been entered:
ALTER TABLE employees ADD COLUMN extension TEXT;
UPDATE employees SET extension = '210' WHERE employee_id = 5;
SELECT first_name, last_name, department, extension
FROM employees
WHERE department = 'Warehouse';
| first_name | last_name | department | extension |
|---|---|---|---|
| Sarah | Long | Warehouse | 210 |
| Felix | Turner | Warehouse | NULL |
The new column sits at the very end of the table, and every existing row holds NULL in it until someone fills in a value. ALTER TABLE only changes the structure; putting values in is UPDATE's job.
A new required column needs a default
If you add a column with NOT NULL, you have to give it a default other than NULL. Otherwise the database wouldn't know what to put in the rows that already exist. The SQLite docs list a few more limits when adding a column: no PRIMARY KEY or UNIQUE on it, and no default of CURRENT_DATE, CURRENT_TIMESTAMP or an expression in parentheses.
Task The warehouse wants to store a minimum stock level per product. Add the column min_stock to the products table (whole number, required, default 20). Furniture is ordered one piece at a time: then set min_stock to 2 for every product in the Furniture category. To check it, the exercise adds a new product without a minimum and tries one with an empty minimum.
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Renaming and dropping
RENAME COLUMN changes the name and nothing else. The column keeps its place and its values, and SQLite also updates the indexes, triggers and views that mention it. SQLite has supported it since version 3.25.0. DROP COLUMN removes a column along with its contents and arrived in version 3.35.0. It refuses when the column is the primary key, is UNIQUE or indexed, or is used in a foreign key, a view or another column's CHECK rule.
Task Tidying up the customers table: sales now keeps phone numbers in the CRM, so the phone column goes. Also, the CRM calls the registration date signup_date, and to keep exports matching, customer_since should have the same name: rename it to signup_date. All other data stays exactly as it is.
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
What ALTER TABLE can't do in SQLite
Changing a column's data type, or adding a CHECK rule or a foreign key afterwards: SQLite has no ALTER TABLE for any of that. Its documentation describes a fixed procedure instead. Boiled down, it looks like this:
CREATE TABLE products_new ( ... the layout you want ... );
INSERT INTO products_new SELECT ... FROM products;
DROP TABLE products;
ALTER TABLE products_new RENAME TO products;
The full version has more steps: switch foreign keys off first, run everything inside one transaction, recreate indexes and views, and check the foreign keys at the end. If you do this on a real database, follow the documented steps exactly and take a backup before you start.
Common mistakes
A required column without DEFAULT
SQLite refuses ALTER TABLE products ADD COLUMN min_stock INTEGER NOT NULL. The exercise reports “The database says: Cannot add a NOT NULL column with default value NULL”. Add DEFAULT 20.
Several changes in one statement
ALTER TABLE customers DROP COLUMN phone, ADD COLUMN email TEXT works in MySQL and PostgreSQL, but not in SQLite. The exercise reports a syntax error near ",". Write two statements separated by a semicolon. Even PostgreSQL only accepts RENAME on its own.
Creating a new column instead of renaming
Add a new column, copy the values across and drop the old one, and you end up with the right data in the wrong place: the new column sits at the end. The exercise then tells you the column order is off. RENAME COLUMN is shorter and leaves everything where it was.
Dropping the primary key
ALTER TABLE customers DROP COLUMN customer_id fails with “The database says: cannot drop PRIMARY KEY column”. That's a good thing, since every order points to its customer through that number.
ALTER COLUMN in SQLite
ALTER TABLE employees ALTER COLUMN salary INTEGER is SQL Server syntax. SQLite doesn't know it, and the exercise rejects the statement. In SQLite you change a data type only by way of a new table, as described above.
ALTER TABLE in MySQL, PostgreSQL, SQL Server and SQLite
| Change | MySQL | PostgreSQL | SQL Server | SQLite |
|---|---|---|---|---|
| add a column | ADD [COLUMN] email VARCHAR(255), position with FIRST or AFTER col | ADD [COLUMN] [IF NOT EXISTS] email text | ADD email nvarchar(255) (no COLUMN) | ADD [COLUMN] email TEXT |
| rename a column | RENAME COLUMN old TO new | RENAME [COLUMN] old TO new | EXEC sp_rename 'table.old', 'new', 'COLUMN' | RENAME [COLUMN] old TO new (3.25.0+) |
| change the type | MODIFY [COLUMN] or CHANGE [COLUMN] | ALTER [COLUMN] col TYPE type | ALTER COLUMN col type | only via a new table |
| drop a column | DROP [COLUMN] col | DROP [COLUMN] [IF EXISTS] col | DROP COLUMN [IF EXISTS] col | DROP [COLUMN] col (3.35.0+) |
| rename the table | RENAME [TO] new | RENAME TO new | EXEC sp_rename 'old', 'new' | RENAME TO new |
Square brackets mean optional. In SQL Server the word COLUMN is required when dropping, and IF EXISTS is available from SQL Server 2016. In MySQL, MODIFY and CHANGE need the complete column definition again; anything you leave out, such as NOT NULL, is not carried over.
Sources: MySQL Reference Manual (ALTER TABLE), PostgreSQL documentation (ALTER TABLE), Microsoft Learn (ALTER TABLE, sp_rename), sqlite.org (ALTER TABLE, release history 3.25.0 and 3.35.0).
Frequently asked questions
Do I lose data when I add a column?
No. Every row stays, and the new column is either empty (NULL) or holds the DEFAULT value.
Can I undo DROP COLUMN?
There's no undo command. In SQLite, ROLLBACK reverses it as long as it ran inside an open transaction; otherwise only a backup helps. The transactions lesson covers this.
What's the difference between ALTER TABLE and UPDATE?
ALTER TABLE changes the structure: which columns exist and what they're called. UPDATE changes the values in existing rows. A new column with content usually takes both, one after the other.
How do I put the new column somewhere else?
MySQL lets you use FIRST or AFTER. In SQLite, according to its docs, a new column always goes at the end. For queries it doesn't matter anyway: your SELECT decides the column order in the result.