SQL ALTER Keyword
ALTER TABLE
The ALTER TABLE
command is used to add, delete, or modify columns in a table. It can also be used to add and delete various constraints in a table.
SQL ALTER Syntax – Add Column
SQL
x
1
ALTER TABLE table_name
2
ADD column_name datatype;
SQL ALTER Example – Add Column
Add a middle_name
column to the Patients
table:
SQL
1
1
ALTER TABLE Patients
2
ADD middle_name VARCHAR(50);

SQL ALTER Syntax – Drop Column
SQL
1
1
ALTER TABLE table_name
2
DROP COLUMN column_name;
SQL ALTER Example – Drop Column
Remove the middle_name
column from the Patients
table:
SQL
1
1
ALTER TABLE Patients
2
DROP COLUMN middle_name;

ALTER COLUMN
The ALTER COLUMN
command is used to change the data type of a column in a table.
SQL ALTER Syntax – Alter Column Data Type
SQL
1
1
ALTER TABLE table_name
2
ALTER COLUMN column_name TYPE new_datatype;
Note: Note: SQLite (our playground) does not support changing a column’s data type directly using
ALTER COLUMN
. Instead, you must:1. Rename the new table.
2. Create a new table with the desired schema.
3. Copy the data from the old table.
4. Drop the old table.
SQL ALTER Example – Alter Column Data Type
Change the data type of the contact_number
column in the Patients
table to a longer VARCHAR
:
SQL
1
1
ALTER TABLE Patients
2
ALTER COLUMN contact_number TYPE VARCHAR(25);
Note: This will not work in our playground.