All Type Coding

Search Here

ALTER COMMAND in sql server.


ALTER COMMAND

The SQL ALTER TABLE command is used to add, delete or modify columns in an existing table. we will see how to use ALTER command command in table on database. ALTER command is mainly used to add, modify and drop columns, indexes and constraints on table in relational database 

1- How to add column in existing table using ALTER command in SQL:

ALTER TABLE table_name ADD column_name datatype;

When this field is added to the table it will contain NULL Value by default.

 2-How to drop existing column from table using ALTER command in SQL:

ALTER TABLE table_name drop column  column_name

 3-How to  change the data type of a column in a table, use the following syntax

ALTER TABLE table_name Alter column  column_name datatype

 4-How to drop Primary Key Constraint using ALTER command in database :

ALTER TABLE table_name drop CONSTRAINT Constraint_name

 5-How to add primary key constraints using ALTER command in SQL:

ALTER TABLE table_name  ADD Constraint  Constraint_name PRIMARY KEY(Col1,Col2..)


The basic syntax of ALTER TABLE to ADD UNIQUE CONSTRAINT to a table is as follows:
ALTER TABLE table_name  ADD Constraint Constraint_name UNIQUE(Col1,Col2…)
The basic syntax of ALTER TABLE to ADD CHECK CONSTRAINT to a table is as follows:
ALTER TABLE table_name  ADD Constraint Constraint_name CHECK(CONDITION);

No comments :

Post a Comment