• September 29, 2014
  • SQL
  • Comments Off on SQL ALTER Table

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.

 
Next Topic: SQL COPY Table with example.
Previous Topic: SQL CREATE Table with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 


Comments are closed.