đ ī¸ The ALTER TABLE statement is a Data Definition Language (DDL) command used to modify the structure of an existing table. It allows you to add, modify, rename, or remove columns, as well as add or remove constraints without recreating the table.
đ What is ALTER TABLE?
After a table has been created, business requirements often change. The ALTER TABLE statement lets you update the table's structure while preserving the existing data whenever possible.
Information
đ¯ Why Use ALTER TABLE?
Database schemas evolve over time, and ALTER TABLE provides a flexible way to make structural changes.
- đ Add new columns.
- đ Modify existing columns.
- đ Rename columns or tables (where supported).
- đ Remove unused columns.
- đ Add or remove constraints.
đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Age |
|---|---|---|---|
| 101 | Alice | Computer Science | 20 |
| 102 | Bob | Mathematics | 21 |
đ Basic Syntax
ALTER TABLE Syntax
ALTER TABLE table_name
operation;â Add a New Column
Use the ADD clause to add a new column to an existing table.
Add a Column
ALTER TABLE Students
ADD Email VARCHAR(150);The updated table structure becomes:
| StudentID | Name | Department | Age | |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 20 | NULL |
| 102 | Bob | Mathematics | 21 | NULL |
âī¸ Modify a Column
You can change a column's data type, size, or other properties. The syntax differs across database systems.
Modify a Column (MySQL)
ALTER TABLE Students
MODIFY Email VARCHAR(255);Alter a Column (SQL Server)
ALTER TABLE Students
ALTER COLUMN Email VARCHAR(255);Important
đ Rename a Column
Many modern database systems support renaming existing columns.
Rename a Column (Standard Style)
ALTER TABLE Students
RENAME COLUMN Name TO FullName;Tip
đī¸ Drop a Column
Remove an unnecessary column from a table.
Drop a Column
ALTER TABLE Students
DROP COLUMN Email;Warning
đ Add a Constraint
Constraints can also be added after a table has been created.
Add a UNIQUE Constraint
ALTER TABLE Students
ADD CONSTRAINT UQ_Students_Email
UNIQUE (Email);đ Drop a Constraint
Constraints can be removed when they are no longer required.
Drop a Constraint
ALTER TABLE Students
DROP CONSTRAINT UQ_Students_Email;đ Add a Foreign Key
Foreign keys establish relationships between tables after they have been created.
Add a Foreign Key
ALTER TABLE Enrollments
ADD CONSTRAINT FK_Enrollments_Students
FOREIGN KEY (StudentID)
REFERENCES Students(StudentID);âī¸ CREATE TABLE vs ALTER TABLE
| Feature | CREATE TABLE | ALTER TABLE |
|---|---|---|
| Purpose | Create a new table. | Modify an existing table. |
| Requires Existing Table | â No | â Yes |
| Add Columns | During creation. | After creation. |
| Modify Columns | â No | â Yes |
đŧ Real-World Example
A university introduces email communication for students. The existing Students table must be updated to store email addresses.
Add Student Email Column
ALTER TABLE Students
ADD Email VARCHAR(150) UNIQUE;The table structure is updated without affecting the existing student records.
đī¸ Database Compatibility
| Database System | Notes |
|---|---|
| MySQL | Uses ADD, MODIFY, and CHANGE for structural changes. |
| PostgreSQL | Supports ALTER COLUMN, RENAME COLUMN, and many advanced options. |
| SQL Server | Uses ALTER COLUMN for modifying columns and supports extensive constraint management. |
| Oracle | Supports adding, modifying, renaming, and dropping columns and constraints. |
| SQLite | Supports only a subset of ALTER TABLE operations, with additional capabilities in newer versions. |
â ī¸ Common Mistakes
- â Changing a column's data type without checking existing data.
- â Dropping a column that contains important information.
- â Adding constraints that conflict with existing records.
- â Assuming every SQL database uses identical ALTER TABLE syntax.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ ALTER TABLE modifies the structure of an existing table.
- đ You can add, modify, rename, or remove columns.
- đ Constraints can be added or removed after table creation.
- đ Syntax varies across different database systems.
- đ Existing data should always be considered before making structural changes.
- đ Test schema changes before applying them to production databases.