Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Constraint in SQL

Why Constraint in SQL: Constraint is using to restrict the insertion of unwanted data
in any columns. We can create constraints on single or multiple columns of any table. It
maintains the data integrity i.e. accurate data or original data of the table. Data integrity
rules fall into three categories:

 Entity integrity
 Referential integrity
 Domain integrity

Entity Integrity: Entity integrity ensures each row in a table is a uniquely identifiable
entity. You can apply entity integrity to a table by specifying a PRIMARY KEY and
UNIQUE KEY constraint.

Referential Integrity: Referential integrity ensures the relationships between tables


remain preserved as data is inserted, deleted, and modified. You can apply referential
integrity using a FOREIGN KEY constraint.

Domain Integrity: Domain integrity ensures the data values inside a database follow
defined rules for values, range, and format. A database can enforce these rules using
CHECK KEY constraints.

Types of constraints in SQL Server:-

1. Unique Key constraint.


2. Not Null constraint.
3. Check constraint
4. Primary key constraint.
5. Foreign Key constraint.

All the above constraints are created in two levels

1. Column level

2. Table level

Column level:-

In this method we are defining constraints for individual columns. That is

whenever we are creating a column then only we are specify the constraint type.

Syntax - Column Level:-

If a constraint is being created at the column level, the constraint applies to the column
specified
Columnname [Contraint Contraintname] Constrainttype,

Table level:-

in this method we are defining constraints on group of columns that is first we


should define all columns then at the last we must specify constraint type with group of
columns. Not null constraint can not be defined.

General Syntax - Table Level

• Approach can be used to create any constraint type except NOT NULL

• Required if constraint is based on multiple columns

[Constraint Constraintname] Constrainttype (columnname,---)

Not null constraint:-

• NOT NULL’ constraints are used to avoid null values for a specific column.

• We can add ‘NOT NULL’ constraint at column level only.

• Columns without the ‘NOT NULL’ constraint allow NULL values. (i.e. if no value is
specified then NULL is the default) .

If Constraint name is not specified then system will generate a unique name for
the constraint.

• Not null should be defined only at column level.

Ex:

Create table test1(sno int not null,name Varchar(10) not null);

Unique constraint:-

Unique constraint is used to avoid duplicates values in the column data but it
allows null values.

We use unique constraint to ensure that our table column has unique information.

• A composite unique key designates a combination of columns as the unique key.

• A composite unique key always declared at the table level.


COLUMN LEVEL:-

SYNTAX:-

<column Name><date type>(<size>)unique

Ex: create table EMP(EID int unique,ENAME varchar(50) unique,SALARY money);

TABLE LEVEL:

Ex: create table EMP(EID int,ENAME varchar(50) ,SALARYmoney,unique(eid,ename));

Primary key constraint:-

Primary Key is used to avoid duplicates and nulls. This constraint will work as a
combination of both unique and not null.

• Only one primary key is allowed to a table.

• If we want more thank one column to work as Primary Key columns then those
are called composite primary key.

• Note that we can take a maximum of 32 columns in composite primary key and
these composite primary key can be declared only at Table level.

COLUMN LEVEL:-

Primary key constraint defined at column level:-

Syntax:-

<column name ><data type>(<size>)primary key

If we do not specify name for the constraint then system will automatically assign a
system generated name.

constraint without name

sql> CREATE TABLE emp7(empno int PRIMARY KEY,

ename VARCHAR(100));

TABLE LEVEL:-

Primary constraint defined at table level:-


Syntax:-

Create table tablename(<columnName1><datatype>(<size>),

<columnName2><datetype>(<size>),

Primary key(<columnName1>,[<columnName2,…..>]));

Ex: Sql> CREATE TABLE emp9(empno int,

ename VARCHAR(100),

PRIMARY KEY(empno));

COMPOSITE PRIMARY KEY

Sql> CREATE TABLE employee (empno int,

name varchar(20),

PRIMARY KEY(empno,name));

Foreign Key: - One of the most important concepts in database is creating


relationships between database tables. These relationships provide a mechanism for
linking data stored in multiple tables and retrieving it in an efficient manner.

In order to create a link between two tables we must specify a foreign key in one table
that references a column in another table.

Foreign key constraint is used for relating or binding two tables with each other and then
verifies the existence of one table data in the other.

To impose a foreign key constraint we require the following things.

We require two tables for binding with each other and those two tables must have a
common column for linking the tables.

COLUMN LEVEL:

Example:

To create Department Table (PARENT TABLE):-

create table Department(Deptno int primary key,DNAME varchar(50),LOCATION


varchar(max))

Insert Records Into Department Table:


insert into Department values(10,'Sales','Chennai')
insert into Department values(20,'Production','Mumbai')
insert into Department values(30,'Finance','Delhi')
insert into Department values(40,'Research','Hyderabad')

To create Employee Table(CHILD TABLE):-

create table Employee(EID int,ENAME varchar(50),SALARY money,Deptno int foreign


key references Department(Deptno))

Insert Records Into Employee Table:

insert into Employee values(101,'Sai',35000,10)


insert into Employee values(102,'Pavan',45000,20)
insert into Employee values(103,'Kamal',74000,30)
insert into Employee values(104,'Ravi',58000,40)

The below records are not allowed in to employee table:

insert into Employee values(105,'Kamal',74000,50)


insert into Employee values(106,'Ravi',58000,60)

When we impose the foreign key constraint and establish relation between the table,the
followiong three rules will come into picture.

Rule1:- Cannot insert a value into the foreign key column provided that value is not
existing under the refernce key column of the parent table.
Rule2:- Cannot update the reference key value of a parent table provided that value
has corresponding child record in the child table with out addressing what to do with the
child record.

Rule3:- Cannot delete a record from the parent table provided that records reference
key value has child record in the child table with out addressing what to do with the child
record.

If we want to delete or update a record in the parent table when they have
corresponding child records in the child table we are provide with a set of rules to
perform delete and update operations knows as cascade rules.

On delete cascade:- It is used to delete a key value in the parent table which is
referenced by foreign key in other table all rows that contains those foreign keys in child
table are also deleted.
On Update cascade:- It is used to Update a key value in the parent table which is
referenced by foreign key in other table all rows that contains those foreign keys in child
table are also updated.

On Update set null: It is used to Update a key value in the parent table which is
referenced by foreign key in other table all rows that contains those foreign keys in child
table are convert into null.

On Delete set null: It is used to delete a key value in the parent table which is
referenced by foreign key in other table all rows that contains those foreign keys in child
table are convert into null

If we apply this rule while creating the child table like below

create table Emp(EID int,ENAME varchar(50),SALARY money,Deptno int foreign key


references Department(Deptno)on delete cascade on update cascade /
on update set null on delete set null)

Ex:-

 update Department set Deptno=222 where Deptno=20

 delete from Department where Deptno=222


TABLE LEVEL:

We can add foreign constraint at table level.

sql> CREATE TABLE emp1(empno int,

ename VARCHAR(100),

deptno int,

FOREIGN KEY(deptno) REFERENCES dept1(deptno));

Making a Relationship between Three Tables


CASE-1:

Create table CUSTOMER (CID Int primary key, CNAME Varchar (20),
MAILID Varchar (40))

Insert customer values (1,'a','a@gmail.com'), (2,'b','b@gmail.com'),


(3,'c','c@gmail.com')

CASE-2

Create table PRODUCTS (PCODE Int primary key, PNAME varchar (50),
PRICE money)

Insert products values (10,'C', 500), (20,'C++’, 1000), (30,'.NET’, 35000),


(40,’SQL’, 1800)

---------------------------------------------------------------------------

CASE-3

Create table ORDERS (ORID Int primary key, ORDATE date, QUANTITY
int,CID Int foreign key references CUSTOMER(cid) on update cascade on
delete cascade, PCODE Int foreign key references PRODUCTS(pcode) on
update cascade on delete cascade)

Insert into ORDERS values (101,’2014/10/15’, 3, 2, 10) //ALLOWED


Insert into ORDERS values (102,’2014/10/25’, 3, 4, 50) //NOTALLOWED

----------------------------------------------------------

Update CUSTOMER set cid=100 where cid=1

Delete from CUSTOMER where cid=100

Update PRODUCTS set orid=21 where orid=20

Delete from PRODUCTS where orid=30

You might also like