DBMSSQL

Alter table command in SQL

Alter table command

Learn to create table

We can change the definition of a table even after creating it. For this, ALTER TABLE command is used. This command can be used to add columns to the table, change their sizes, change their datatypes or add or delete constraints.

The syntax for ALTER TABLE command is as mentioned below:

ALTER TABLE  <TABLE NAME > [options]

ADD

Add keyword is used to add columns or constraints to the table. For example to a column to an existing table we use following command

ALTER TABLE  TABLE NAME  ADD COLUMN  NAME  DATA TYPE(SIZE);

Example.

ALTER TABLE EMPLOYEE ADD PHONE CHAR(10);

MODIFY

Modify option can change the data type, column width and constraints of an existing column. For example to change the column width and data type  of phone of employee table, we can use the following statement.

ALTER TABLE EMPLOYEE MODIFY PHONE NUMBER(10)

DROP

To remove the constraints imposed on the table, drop option can be used. For example the following statement will remove the constraint check_code

ALTER TABLE EMPLOYEE DROP CONSTRAINT CHECK_CODE

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button