Alter Table in SQL

To add, delete, or modify a table column, use the ALTER TABLE statement. It can also be used to rename a table as well as add and remove constraints.

Syntax of ALTER TABLE to add a new column:

ALTER TABLE tableName ADD columnName datatype;

Example:

Syntax of ALTER TABLE to add multiple columns:

ALTER TABLE tableName ADD
 
(columnName1 datatype,
 
columnName2 datatype,
 
…
 
columnNameN datatype);

Example:

ALTER TABLE EMPLOYEE ADD (ADDRESS VARCHAR (20));

Output:

Table altered.

Syntax of ALTER TABLE to modify a column:

ALTER TABLE tableName MODIFY columnNme datatype;  

Example:

ALTER TABLE EMPLOYEE MODIFY ADDRESS VARCHAR (25);

Output:

Table altered.

Syntax of ALTER TABLE to drop a column:

ALTER TABLE tableName DROP COLUMN columnName; 

Example:

ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS;

Output:

Table dropped.

Syntax of ALTER TABLE to rename a column:

ALTER TABLE tableName  RENAME COLUMN oldColumnName to newColumnName; 

Example:

ALTER TABLE EMPLOYEE RENAME COLUMN NAME to EMP_NAME;

Output:

Table altered.

Syntax of ALTER TABLE to DROP CONSTRAINT from a table:

ALTER TABLE tableName DROP CONSTRAINT constraintName;

Example:

ALTER TABLE EMPLOYEE DROP CONSTRAINT SYS_C004697;

Output:

Table dropped.