SQL Learning
SQL ALTER TABLE Statement
SQL Learning
SQL ALTER TABLE Statement
Learn how to use ALTER TABLE to add, remove, or rename columns and modify existing database schemas.
What Is the ALTER TABLE Statement?
The ALTER TABLE
command allows you to change the structure of an existing table. This includes adding new columns, renaming existing ones, changing data types, or even dropping constraints.
General Syntax
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;
ALTER TABLE table_name
DROP COLUMN column_name;
Examples from the TokTuk Dataset
Add a New Column
-- Add a bio column to Users
ALTER TABLE Users
ADD bio TEXT;
Rename a Column
-- Rename upload_date to published_at in Videos
ALTER TABLE Videos
RENAME COLUMN upload_date TO published_at;
Drop a Column
-- Remove the bio column
ALTER TABLE Users
DROP COLUMN bio;
Common ALTER TABLE Use Cases
- Adding audit columns like
updated_at
- Renaming fields to match naming conventions
- Dropping columns that are no longer used
- Changing data types (e.g.,
INT
toBIGINT
)
Tips for ALTER TABLE
- Use with caution—schema changes can impact your whole app!
- Make a backup or test on staging before altering production tables.
- Keep column naming consistent and descriptive.
- Check your database engine's docs—support varies slightly across SQL dialects.