You are on page 1of 6

Constraints

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

Not NULL + Unique

Uniquely identifies each record into the table.

Primary key is used in general with numeric value.

Create table prime (P_ID int primary key, P_name varchar(10),Pcity varchar(10))

insert into prime values(1,'Praveen','pune')

insert into prime values(2,'Praveen','pune')

insert into prime values(3,'mohit','Jaipur')

select * from prime

2.foreign key

Used to maintain Referential integrity

it ensure that all values in a columns are diffrent.

NULL value can be applied at column level or table column.

It means that it can accept one NULL value

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(

D_ID int primary key,

Dept_Name varchar(20))

insert into department values(1,'HR')

insert into department values(2,'Finance')

insert into department values(3,'Account')

insert into department values(4,NULL)

create table emp(

E_ID int primary key,

E_Name varchar(10),

D_ID int foreign key references department(D_ID))

insert into emp values(11,'Amit',1)

insert into emp values(12,'rohit',2)

insert into emp values(13,'Mohit',3)

insert into emp values(14,'Mohit',NULL)


insert into emp values(15,'ronit',NULL)

The below statement through an exception

insert into emp values(17,'shon',5)

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'

select * from department

select * from emp

Q.How to check the number of tables

select * from INFORMATION_SCHEMA.tables

NULL value

A column with a NULL value is a column with No value.

NULL value is diffrent from a '0(Zero)' value and 'Blank/Empty' space.

Q, How to test for NULL values in column?

There are two ways to check the NULL values present in column or not

1.IS NULL

2.IS NOT NULL

select * from employee where ELOC is null

select * from employee where EDEPT is null


select * from employee where EDEPT is not null

3.NOT NULL Key

NOT NULL Constraint is restricts a column from having a 'NULL' value.

Once if you defined/Applied NOT NULL constarints on column level then you cant insert NULL values.

create table NOTNULL(

NID int primary key,

NNAME varchar(10) NOT NULL,

Age int NOT NULL)

Method-I

insert into NOTNULL values (1,'Kate',42)

insert into NOTNULL values (2,NULL,42)

insert into NOTNULL values (2,'Kate',42)

insert into NOTNULL values (3,'',56)

Method-II

insert into NOTNULL (NID,NNAME) values (4,'Shon')

The above statement through an exception

Exception/Error-Cannot insert the value NULL into column 'Age', table 'Testing18.dbo.NOTNULL';
column does not allow nulls. INSERT fails.

select * from NOTNULL


4.Unique key

It ensures that all the columns sould have UNIQUE values or diffrent value.

One NULL/One Blank/empty value can be inserted at column level.

It means that it can not accepet NULL or Blank/Empty space twice into column

create table UNIQUE_Table(

U_ID int primary key,

U_Name varchar(10) Unique,

U_LOC varchar(10) NOT NULL Unique )

select * from UNIQUE_Table

insert into UNIQUE_Table values(1,'Niraj','Pune')

insert into UNIQUE_Table values(2,'','Nasik')

insert into UNIQUE_Table values(3,NULL,'Mumbai')

insert into UNIQUE_Table values(4,'Shrikant','Patna')

insert into UNIQUE_Table values(5,'Mohan',NULL)

5.Check key

It ensures that all values in a column statisfies a specific condition.

Check constarints is used to restrict the value of a column.

It is just like condition checking before inserting the data into column.

Create table CHECK_KEY(

C_ID int primary key ,

C_Name varchar(10) NOT NULL UNIQUE,

C_AGE int check(C_AGE >18))


insert into CHECK_KEY values(1,'Sumit',19)

The below statment through an exception while inserting the data

insert into CHECK_KEY values(2,'Ronit',17)

Exception/Error

The INSERT statement conflicted with the CHECK constraint


"CK__CHECK_KEY__C_AGE__440B1D61".

The conflict occurred in database "Testing18", table "dbo.CHECK_KEY", column 'C_AGE'.

6.Default constarint

Set a default value to column when value is not defined/inserted/specified.

Create table DEFAULT_VALUE(

D_ID int primary key,

D_name varchar(10) NOT NULL Unique,

D_City varchar(10),

D_AGE int check(D_age >=20),

D_LOC varchar(20) default 'Balaji Nagar')

select * from DEFAULT_VALUE

METHOD-I

insert into DEFAULT_VALUE values(1,'Smita','Jaipur',20,'katraj')

insert into DEFAULT_VALUE values(2,'Amla','Chennai',28,default)

insert into DEFAULT_VALUE values(3,'Asin','Madurai',34,'')

You might also like