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_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-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:

ChangeStatement
add a columnALTER TABLE customers ADD COLUMN email TEXT;
rename a columnALTER TABLE employees RENAME COLUMN salary TO annual_salary;
rename the tableALTER TABLE order_items RENAME TO order_lines;
drop a columnALTER 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_namelast_namedepartmentextension
SarahLongWarehouse210
FelixTurnerWarehouseNULL

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_idnamecategorypricestock
1Copy paper, 500 sheetsPaper4.99420
2Blue ballpoint pens, 10-packWriting3.49310
3Lever arch file, grayFiling2.79185
4Comfort office chairFurniture18912
5Standing deskFurniture4495
6Black ink cartridgePrinter supplies24.964
7Yellow sticky notes, 12 padsWriting6.990
8Mono laser printer L30Electronics2198
927-inch monitorElectronics27915
10Notebook A5, ruledPaper5.49140
11Black toner XLPrinter supplies89.922
12Cross-cut shredderElectronics1290

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_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-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

ChangeMySQLPostgreSQLSQL ServerSQLite
add a columnADD [COLUMN] email VARCHAR(255), position with FIRST or AFTER colADD [COLUMN] [IF NOT EXISTS] email textADD email nvarchar(255) (no COLUMN)ADD [COLUMN] email TEXT
rename a columnRENAME COLUMN old TO newRENAME [COLUMN] old TO newEXEC sp_rename 'table.old', 'new', 'COLUMN'RENAME [COLUMN] old TO new (3.25.0+)
change the typeMODIFY [COLUMN] or CHANGE [COLUMN]ALTER [COLUMN] col TYPE typeALTER COLUMN col typeonly via a new table
drop a columnDROP [COLUMN] colDROP [COLUMN] [IF EXISTS] colDROP COLUMN [IF EXISTS] colDROP [COLUMN] col (3.35.0+)
rename the tableRENAME [TO] newRENAME TO newEXEC 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.

Up next: primary and foreign keys

Keys tie tables together: every order points to exactly one customer through the customer ID. The next lesson shows how to set up those links and keep them safe.

Practice keys

Write to us

Choose language

Select your preferred language