SQL ALTER TABLE Get Certified
Learn how to modify the structure of an existing table – add, rename or drop columns, and rename the table itself – with the ALTER TABLE statement.
What is ALTER TABLE?
ALTER TABLE changes the definition of a table that already exists.
You do not have to recreate the table; the existing data is preserved (except when a column is dropped).
SQLite supports the following common operations:
ADD COLUMNRENAME COLUMNRENAME TO(rename the table)DROP COLUMN(modern SQLite versions)
Basic Syntax – Add a Column
ALTER TABLE table_name
ADD COLUMN column_name data_type;
Adding a New Column
What this code does: Creates a gadgets table, adds a category column, fills the new values and displays the result.
DROP TABLE IF EXISTS gadgets;
CREATE TABLE gadgets (
gadget_id INTEGER PRIMARY KEY,
gadget_name TEXT,
price REAL
);
INSERT INTO gadgets (gadget_name, price) VALUES
('Smart Lamp', 1299.00),
('Fitness Band', 2499.00),
('Desk Fan', 1799.00);
ALTER TABLE gadgets
ADD COLUMN category TEXT;
UPDATE gadgets
SET category = 'Home'
WHERE gadget_id IN (1, 3);
UPDATE gadgets
SET category = 'Wearable'
WHERE gadget_id = 2;
SELECT *
FROM gadgets
ORDER BY gadget_id;
Adding a Column with a Default Value
Supplying a DEFAULT value automatically fills the new column for every existing row.
What this code does: Adds a status column that defaults to “Planned” for every project already in the table.
DROP TABLE IF EXISTS projects;
CREATE TABLE projects (
project_id INTEGER PRIMARY KEY,
project_name TEXT NOT NULL
);
INSERT INTO projects (project_name) VALUES
('Website Redesign'),
('Mobile App'),
('Data Dashboard');
ALTER TABLE projects
ADD COLUMN status TEXT DEFAULT 'Planned';
SELECT *
FROM projects
ORDER BY project_id;
Renaming a Column
RENAME COLUMN changes the name of an existing column without affecting its data.
What this code does: Renames the column full_name to member_name.
DROP TABLE IF EXISTS members;
CREATE TABLE members (
member_id INTEGER PRIMARY KEY,
full_name TEXT,
city TEXT
);
INSERT INTO members (full_name, city) VALUES
('Riya Sharma', 'Delhi'),
('Arjun Mehta', 'Pune'),
('Nisha Kapoor', 'Jaipur');
ALTER TABLE members
RENAME COLUMN full_name TO member_name;
SELECT *
FROM members
ORDER BY member_id;
Renaming a Table
RENAME TO changes the name of the whole table.
What this code does: Renames old_products to products and then queries the new name.
DROP TABLE IF EXISTS old_products;
DROP TABLE IF EXISTS products;
CREATE TABLE old_products (
product_id INTEGER PRIMARY KEY,
product_name TEXT,
price REAL
);
INSERT INTO old_products VALUES
(1, 'Notebook', 120.00),
(2, 'Backpack', 950.00),
(3, 'Water Bottle', 450.00);
ALTER TABLE old_products
RENAME TO products;
SELECT *
FROM products
ORDER BY product_id;
Dropping a Column
Modern SQLite versions support DROP COLUMN, which permanently removes the column and its data.
What this code does: Removes the temporary_code column from the employees table.
DROP TABLE IF EXISTS employees;
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
employee_name TEXT,
department TEXT,
temporary_code TEXT
);
INSERT INTO employees VALUES
(1, 'Aarav', 'Engineering', 'TMP101'),
(2, 'Meera', 'Design', 'TMP102'),
(3, 'Kabir', 'Research', 'TMP103');
ALTER TABLE employees
DROP COLUMN temporary_code;
SELECT *
FROM employees
ORDER BY employee_id;
Adding Several Columns
SQLite allows only one column per ADD COLUMN statement, so multiple columns require separate ALTER TABLE commands.
What this code does: Adds event_date and location columns to an events table and then fills the new values.
DROP TABLE IF EXISTS events;
CREATE TABLE events (
event_id INTEGER PRIMARY KEY,
event_name TEXT
);
INSERT INTO events (event_name) VALUES
('Science Fair'),
('Tech Expo'),
('Design Workshop');
ALTER TABLE events
ADD COLUMN event_date TEXT;
ALTER TABLE events
ADD COLUMN location TEXT;
UPDATE events
SET event_date = '2026-09-10', location = 'Delhi'
WHERE event_id = 1;
UPDATE events
SET event_date = '2026-09-15', location = 'Mumbai'
WHERE event_id = 2;
UPDATE events
SET event_date = '2026-09-20', location = 'Pune'
WHERE event_id = 3;
SELECT *
FROM events
ORDER BY event_id;
Adding a Column with a CHECK Constraint
A newly added column may include a CHECK constraint (provided existing data would not violate it).
What this code does: Adds a score column that must be between 1 and 5, then populates it.
DROP TABLE IF EXISTS ratings;
CREATE TABLE ratings (
rating_id INTEGER PRIMARY KEY,
item_name TEXT NOT NULL
);
INSERT INTO ratings (item_name) VALUES
('Camera'),
('Headphones'),
('Monitor');
ALTER TABLE ratings
ADD COLUMN score INTEGER CHECK (score BETWEEN 1 AND 5);
UPDATE ratings SET score = 5 WHERE rating_id = 1;
UPDATE ratings SET score = 4 WHERE rating_id = 2;
UPDATE ratings SET score = 3 WHERE rating_id = 3;
SELECT *
FROM ratings
ORDER BY rating_id;
Inspecting the Modified Structure
After any structural change you can examine the table definition with PRAGMA table_info().
What this code does: Adds a brand column, renames device_name to model_name, then lists the final column definitions.
DROP TABLE IF EXISTS devices;
CREATE TABLE devices (
device_id INTEGER PRIMARY KEY,
device_name TEXT,
price REAL
);
ALTER TABLE devices
ADD COLUMN brand TEXT;
ALTER TABLE devices
RENAME COLUMN device_name TO model_name;
SELECT
cid,
name,
type,
"notnull",
dflt_value,
pk
FROM pragma_table_info('devices')
ORDER BY cid;
ALTER TABLE Operations Summary
ALTER TABLE vs UPDATE
Important Points
ALTER TABLEmodifies an existing table without recreating it.ADD COLUMNcan include a DEFAULT value or a CHECK constraint.RENAME COLUMNandRENAME TOchange names while preserving data.DROP COLUMNpermanently removes the column and its values.- In SQLite only one column may be added per ALTER TABLE statement.
Best Practices
- Back up important data before any structural change.
- Verify that existing data will not violate new constraints.
- Choose clear, meaningful names when renaming columns or tables.
- Test ALTER statements on a development copy first.
- Be especially careful with DROP COLUMN – the data cannot be recovered without a backup.
The ALTER TABLE statement lets you evolve a table’s structure – add, rename or remove columns, or rename the table itself – without losing the data that is already stored. Click Test My Code on any example to run it live, or Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.