Professional Documents
Culture Documents
Constraints are used to maintain the accuracy and integrity of the data.
1. Primary Key
2. Foreign Key
3. Null key
4. Unique key
5. Check key
6. Default key
1.Primary Key
Create table prime (P_ID int primary key, P_name varchar(10),Pcity varchar(10))
2.foreign key
A foriegn key is a column or collection of columns in one table that refers to the primary key
in another table.
Create table department(
Dept_Name varchar(20))
E_Name varchar(10),
Error/Exception -The INSERT statement conflicted with the FOREIGN KEY constraint
"FK__emp__D_ID__3A81B327". The conflict occurred in database "Testing18", table
"dbo.department", column 'D_ID'
NULL value
There are two ways to check the NULL values present in column or not
1.IS NULL
Once if you defined/Applied NOT NULL constarints on column level then you cant insert NULL values.
Method-I
Method-II
Exception/Error-Cannot insert the value NULL into column 'Age', table 'Testing18.dbo.NOTNULL';
column does not allow nulls. INSERT fails.
It ensures that all the columns sould have UNIQUE values or diffrent value.
It means that it can not accepet NULL or Blank/Empty space twice into column
5.Check key
It is just like condition checking before inserting the data into column.
Exception/Error
6.Default constarint
D_City varchar(10),
METHOD-I