ALTER TABLE
Reshaping Your Database
Learn to modify existing table structures by adding columns, removing columns, renaming, and changing data types: all without losing your data.
Why Do We Need ALTER TABLE?
When you first create a table with CREATE TABLE, you define its initial structure. But real-world requirements change constantly: new features need new columns, old columns become irrelevant, and data types may need adjusting.
What Is a Table Schema?
A schema is the blueprint of your table. It defines the column names, data types, and constraints that govern what data can be stored.
The ALTER TABLE Command Family
ALTER TABLE is not a single command: it's a family of sub-commands, each targeting a different kind of schema change.
Core Syntax Pattern
ACTION details;
ADD COLUMN: Expanding Your Table
ADD phone_number VARCHAR(20);
This adds a new column called phone_number to the students table. Existing rows will have NULL in this column (unless you set a default value).
DROP COLUMN: Removing Data Permanently
DROP COLUMN phone_number;
This permanently removes the phone_number column and all data stored in it. There is no undo for this operation.
RENAME: Changing Names
ALTER TABLE students
RENAME COLUMN name TO full_name;
-- Rename the entire table
ALTER TABLE students
RENAME TO enrolled_students;
MODIFY: Changing Data Types
ALTER TABLE students
MODIFY age SMALLINT;
-- PostgreSQL syntax
ALTER TABLE students
ALTER COLUMN age TYPE SMALLINT;
ALTER vs CREATE vs DROP
| ALTER TABLE | CREATE TABLE | DROP TABLE | |
|---|---|---|---|
| Purpose | Modify structure | Build new table | Delete entire table |
| Data Impact | Preserves data | Starts empty | Destroys all data |
| Reversible? | Partially | N/A | No |
| Use When | Schema evolves | New table needed | Table is obsolete |
Step-by-Step Approach
Example 1: Adding a Column
Scenario: You have a products table and need to track each product's weight for shipping calculations.
ADD weight_kg DECIMAL(5,2) DEFAULT 0.00;
Example 2: Dropping a Column
Scenario: Your team decides the fax_number column in the contacts table is obsolete and should be removed.
DROP COLUMN fax_number;
Example 3: Multiple Changes at Once
Scenario: You need to add a created_at timestamp, rename name to full_name, and drop the obsolete nickname column: all on the users table.
ALTER TABLE users
ADD created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
RENAME COLUMN name TO full_name,
DROP COLUMN nickname;
Common Mistakes to Avoid
Summary: ALTER TABLE Cheat Sheet
| Action | Syntax | Data Impact |
|---|---|---|
| Add Column | ADD col_name TYPE | Safe: NULLs or defaults |
| Drop Column | DROP COLUMN col_name | Destructive: data lost |
| Rename Column | RENAME COLUMN old TO new | Safe: breaks references |
| Rename Table | RENAME TO new_table | Safe: breaks references |
| Modify Type | MODIFY col TYPE / ALTER COLUMN col TYPE | Caution: may fail on incompatible data |
Question 1 of 5
Which SQL command would you use to add a new column called email of type VARCHAR(255) to an existing table called employees?
Question 2 of 5
What happens to existing data in a column when you run DROP COLUMN?
Question 3 of 5
You want to rename the column fname to first_name in the customers table. Which statement is correct?
Question 4 of 5
When you ADD a new column to a table that already has 1,000 rows, what value will the new column contain in those existing rows (if no DEFAULT is specified)?
Question 5 of 5
Which of the following is NOT a valid ALTER TABLE action?