SQL ALTER TABLE statement can be used to rename a table as well as to add, modify and delete columns of the table. The SQL ALTER TABLE statement is also used to add and drop various constraints on an existing table.
Syntax for various SQL ALTER TABLE is given below:
1. To add a column:
ADD is used to add columns into the existing table. Sometimes we may require to add additional information, in that case we do not require to create the whole database again, ADD comes to our rescue.
ALTER TABLE table_name ADD column_name data_type;
ALTER TABLE student ADD Address VARCHAR(255);
2. To modify a column:
It is used to modify the existing columns in a table. Multiple columns can also be modified at once.
ALTER TABLE table_name MODIFY column_name column_type;
ALTER TABLE student MODIFY Student_ID int NOT NULL;
3. To drop or delete a column:
DROP COLUMN is used to drop column in a table. Deleting the unwanted columns from the table.
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE student DROP COLUMN City;
4. To rename a column:
ALTER TABLE table_name RENAME COLUMN old_name to new_name;
ALTER TABLE student RENAME COLUMN Address to FULL_ADDRESS;
5. To rename a table:
RENAME TABLE existing_table_name TO new_table_name
RENAME TABLE student TO student_A