01 / 21

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.

SQL

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.

id name email age INT VARCHAR VARCHAR INT 1 Alice alice@mail.com 17 2 Bob bob@mail.com 18 Schema: column names + data types Data: the actual rows stored in the table SCHEMA DATA

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.

ADD COLUMN
Insert a new column into an existing table
DROP COLUMN
Remove a column and all its data permanently
RENAME
Change a column name or the table name itself
MODIFY / ALTER
Change a column's data type or constraints

Core Syntax Pattern

ALTER TABLE table_name
  ACTION details;
ALTER TABLE table_name ACTION details Keyword Target Table ADD, DROP, RENAME...

ADD COLUMN: Expanding Your Table

ALTER TABLE students
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

ALTER TABLE students
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

-- Rename a column
ALTER TABLE students
RENAME COLUMN name TO full_name;

-- Rename the entire table
ALTER TABLE students
RENAME TO enrolled_students;

MODIFY: Changing Data Types

-- MySQL syntax
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

1
Identify the change. What exactly needs to change: a new column, a removed column, a renamed field, or a type change?
2
Check dependencies. Do any views, indexes, or application queries reference the column or table you're modifying?
3
Back up if destructive. For DROP COLUMN or type changes, always back up your data first.
4
Write and run the ALTER TABLE statement. Choose the correct sub-command (ADD, DROP, RENAME, MODIFY).
5
Verify with DESCRIBE. Run DESCRIBE table_name; to confirm the schema matches your intention.

Example 1: Adding a Column

Scenario: You have a products table and need to track each product's weight for shipping calculations.

ALTER TABLE products
ADD weight_kg DECIMAL(5,2) DEFAULT 0.00;
BEFORE id name price 1 Widget 9.99 2 Gadget 19.99 AFTER id name price weight_kg 1 Widget 9.99 0.00 2 Gadget 19.99 0.00 New column added with DEFAULT value for all existing rows

Example 2: Dropping a Column

Scenario: Your team decides the fax_number column in the contacts table is obsolete and should be removed.

ALTER TABLE contacts
DROP COLUMN fax_number;
BEFORE id name phone fax_number 1 Alice 555-0101 555-0102 2 Bob 555-0201 NULL AFTER id name phone 1 Alice 555-0101 2 Bob 555-0201 fax_number column and all its data are gone permanently

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.

-- Some databases support multiple actions:
ALTER TABLE users
  ADD created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  RENAME COLUMN name TO full_name,
  DROP COLUMN nickname;

Common Mistakes to Avoid

Dropping Without Backup
Running DROP COLUMN on production data without a backup. This data is gone forever.
Forgetting Dependencies
Renaming a column breaks all queries, views, and indexes that reference the old name.
Wrong Syntax for DB
Using MODIFY (MySQL) in PostgreSQL, or ALTER COLUMN TYPE (PostgreSQL) in MySQL. Check your database docs.

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?