SQL ALTER Table

The ALTER TABLE statement is used to add, delete or modify a table column. It can also be used to rename a table and to add or drop constraints on a table.

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.

No comments: