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); |
ALTER TABLE tableName ADD
(columnName1 datatype,
columnName2 datatype,
…
columnNameN datatype);
Example:
ALTER TABLE EMPLOYEE ADD (ADDRESS VARCHAR (20)); |
ALTER TABLE EMPLOYEE ADD (ADDRESS VARCHAR (20));
Output:
Syntax of ALTER TABLE to modify a column:
ALTER TABLE tableName MODIFY columnNme datatype;
Example:
ALTER TABLE EMPLOYEE MODIFY ADDRESS VARCHAR (25); |
ALTER TABLE EMPLOYEE MODIFY ADDRESS VARCHAR (25);
Output:
Syntax of ALTER TABLE to drop a column:
ALTER TABLE tableName DROP COLUMN columnName;
Example:
ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS; |
ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS;
Output:
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; |
ALTER TABLE EMPLOYEE RENAME COLUMN NAME to EMP_NAME;
Output:
Syntax of ALTER TABLE to DROP CONSTRAINT from a table:
ALTER TABLE tableName DROP CONSTRAINT constraintName;
Example:
ALTER TABLE EMPLOYEE DROP CONSTRAINT SYS_C004697; |
ALTER TABLE EMPLOYEE DROP CONSTRAINT SYS_C004697;
Output:
Next Topic: SQL COPY Table with example.
Previous Topic: SQL CREATE Table with example.