Constraints in Sqlserver: Constraints are to check the type of data is consistent for the field when inserted or modified. It is used to enforce Data integrity. It allows you to have the consistent and accurate values in a Database
Types of constraints are broadly classified in sqlserver are
Primary Key constraint Unique Key constraint Foreign Key constraint Check constraint Primary Key constraint: Primary key will check and it will not allow the Duplicate values and null values for the specified field. A Table can have only one primary key. A primary key can be created on the creation of the table or later also we can modify the table
Syntax for Primary Key
Create table <Table_Name> ( <Column_name1> <Data_type1> IDENTITY(1,1) NOT NULL PRIMARY KEY, <Column_name2> <Data_type2>, <Column_name3> <Data_type3> etc.,) Once the table is create we can alter the table
Alter table <Table_name> Add constraint <Constraint_name> Primary Key(Column_name)
Unique Key
Unique Key will not allow the Duplicate values and it will allow the null values. There can be any number of Unique key. Creating a primary key will automatically create a unique key.
Syntax
Create table <Table_Name> ( <Column_name> <Data_type> NOT NULL Unique, <Column_name2> <Data_type2>, <Column_name3> <Data_type3> etc.,)
Once the table is create we can alter the table
Alter table <Table_name> Add constraint <Constraint_name> Unique(Column_name)
|
| Author: Sriram 02 Nov 2008 | Member Level: Gold Points : 1 |
u r explain only primary key and unique what about remaining i meen check,foreign key.
sriramRamaswamy
|