T-SQL ALTER TABLE

Posted In database - By admin On Thursday, July 30th, 2009 With 0 Comments

The ALTER TABLE statement can be used to modify an existing table by adding, altering, or dropping columns and indexes.

Syntax:- Add Column

ALTER TABLE table_Name
{
ADD
{
column_name data_type [ ( size ) ]
[ DEFAULT value ]
{ [ NULL | NOT NULL ] | [ { PRIMARY KEY | UNIQUE } ] }
{ [ CONSTRAINT constraint_name ] }
}
}

Examples

Code:

ALTER TABLE Product
ADD UnitPrice MONEY;

Output:

The command(s) completed successfully.

Syntax:- Alter Column

ALTER TABLE table_Name
{
ALTER COLUMN column_name
{
data_type [ ( size ) ] [ ( precision [ , scale ] ) ]
[ NULL | NOT NULL ]
}
}

Examples

Code:

ALTER TABLE Product
ALTER COLUMN Item_Count INT;

Output:

(1 row(s) affected)

Syntax:- Delete Column

ALTER TABLE table_Name
{
DROP { COLUMN column_name | [ CONSTRAINT ] constraint_name }
}

Examples

Code:

ALTER TABLE Product
DROP COLUMN Discount_Price;

Output:

The command(s) completed successfully.

Tags:

About -

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>