Saturday, February 7, 2015

SQL ALTER TABLE


SQL ALTER TABLE


The ALTER TABLE statement is used to add, modify or delete columns in an existing table. It is also used to rename a table.
You can also use SQL ALTER TABLE command to add and drop various constraints on an existing table.

SQL ALTER TABLE Add Column

If you want to add columns in SQL table, the SQL alter table syntax is given below:
  1. ALTER TABLE table_name ADD column_name column-definition;  
If you want to add multiple columns in table, the SQL table will be
  1. ALTER TABLE table_name   
  2. ADD (column_1 column-definition,  
  3.            column_2 column-definition,  
  4.            .....  
  5.            column_n column-definition);  

SQL ALTER TABLE Modify Column

If you want to modify an existing column in SQL table, syntax is given below:
  1. ALTER TABLE table_name MODIFY column_name column_type;  
If you want to modify multiple columns in table, the SQL table will be
  1. ALTER TABLE table_name   
  2. MODIFY (column_1 column_type,  
  3.                   column_2 column_type,  
  4.                  .....  
  5.                   column_n column_type);  

SQL ALTER TABLE DROP Column

The syntax of alter table drop column is given below:
  1. ALTER TABLE table_name DROP COLUMN column_name;  

SQL ALTER TABLE RENAME Column

The syntax of alter table rename column is given below:
  1. ALTER TABLE table_name  
  2. RENAME COLUMN old_name to new_name;  

No comments:

Post a Comment