The ALTER statement is used to modify the structure of an existing table in a database.
👉 It allows you to:
ALTER TABLE table_name
operation;
Used to add a new column to an existing table.
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE Students
ADD Email VARCHAR(100);
A new column Email is added to the Students table.
ALTER TABLE table_name
ADD column1 datatype,
ADD column2 datatype;
ALTER TABLE Students
ADD City VARCHAR(50),
ADD Phone VARCHAR(15);
Used to change the datatype or size of a column.
ALTER TABLE table_name
MODIFY column_name new_datatype;
ALTER TABLE Students
MODIFY Name VARCHAR(100);
Changes the size of the Name column.
Used to rename a column and optionally change its datatype.
ALTER TABLE table_name
CHANGE old_column_name new_column_name datatype;
ALTER TABLE Students
CHANGE Name FullName VARCHAR(100);
ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;
ALTER TABLE Students
RENAME COLUMN Age TO StudentAge;
Used to delete a column from a table.
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE Students
DROP COLUMN Phone;
Used to rename an existing table.
ALTER TABLE old_table_name
RENAME TO new_table_name;
ALTER TABLE Students
RENAME TO StudentDetails;
Used to add constraints like PRIMARY KEY, UNIQUE, etc.
ALTER TABLE table_name
ADD CONSTRAINT constraint_name constraint_type (column_name);
ALTER TABLE Students
ADD CONSTRAINT pk_id PRIMARY KEY (ID);
ALTER TABLE table_name
DROP PRIMARY KEY;
ALTER TABLE Students
DROP PRIMARY KEY;
ALTER TABLE table_name
ALTER column_name SET DEFAULT value;
ALTER TABLE Students
ALTER Age SET DEFAULT 18;
| Operation | Purpose |
|---|---|
| ADD COLUMN | Add new column |
| MODIFY | Change datatype |
| CHANGE | Rename + modify column |
| RENAME COLUMN | Rename column |
| DROP COLUMN | Delete column |
| RENAME TABLE | Rename table |
| ADD CONSTRAINT | Add rules (PK, UNIQUE) |
| DROP CONSTRAINT | Remove rules |
ALTER.The ALTER statement is essential for:
👉 It allows databases to evolve without deleting existing data.