Constraints
Constraints are used to give
conditions or rules on the table so that valid/meaning full data can be stored
in table.
·
If the constraint is given after the column name
and data type then it is called as column level constraint.
·
If the
constraint is given at the end of the table or after all the columns in the
table then it is called table level constraint.
·
If we want to give one constraint for two
columns in the table then table level constraint can be used.
Syntax:
constraint<constraint_name>
[unique/not null/primary key]
[check(condition)]
[foriegnkey(column) references
<table_name>(columnname)]
1.Unique
It will accepts new values and
will accepts one null value but it won’t accept duplicate values.
create table emp5
(
enointconstraint
eno_uni unique,
enamevarchar(10)
)
insert into emp5
values(100,'srinivas') ‘accepted because no row is
there in the table with eno 100
select * from emp5
insert into emp5
values(100,'srinivas')’invalidbecause we are inserting
second 100 value in eno
insert into emp5
values(101,'srinivas')’valid
insert into emp5
values(null,'srinivas')‘valid
insert into emp5
values(null,'srinivas')‘invalid-because we are
inserting second null value in eno
2.Not null
It will not accept null values but it will accept duplicate values.
Example:
create table emp6
(enointconstraint eno_NNul not null,
enamevarchar(10)
)
insert into emp6
values(100,'srinivas')‘valid
insert into emp6
values(100,'srinivas')’valid
insert into emp6
values(null,'srinivas')’in valid it won’t accept Null
value
3.PrimaryKey
It is a combination of
unique& not null constraint it will accept new values and will not accept
null values and duplicate values .A table can have only one Primary key.
Example:
create table emp7
(enointconstraint eno_PK primary key,
enamevarchar(10)
)
insert into emp7
values(100,'srinivas') ‘valid
insert into emp7
values(100,'srinivas') ‘in valid it wont accept
duplicate value
insert into emp7
values(null,'srinivas') ‘Invalid it wont accept null
value
4.Check:
Any type of condition can be
given by using check constraint.
***** complex validations it can’t perform,for that we
need to use triggers,constraint can look for the condition that we are
specified it won’t validate with existing data from database.
create table emp8
(
enoint constraint check_eno
check(eno>100 and eno<200),
enamevarchar(10)
)
insert into emp8
values(150,'srinivas')’valid
insert into emp8
values(250,'srinivas')’in valid failed at validation
insert into emp8
values(10,'srinivas')’in valid failed at validation
Giving multiple constraints to the table:
Example:
Create table emplist7
(
Enointconstraint prkey2 primary key,constrainteno_chk check(eno between
1000 and 2000),
Enamevarchar(10)
)
Insert into emplist7
values(1000,’Srinivas’)’valid
Insert into emplist7 values(1001,’Ajay’)’valid
The below insertions will be
failed:
Insert into emplist7 values(1001,’Suresh’)
Insert into emplist7 values(null,’Ravi)
Insert into emplist7 values(900,’Raj’)
5. Foriegn key:
It is used to create relation b/w tables and used to maintain
referential data integrity.
Example:
create table department
(
dnointconstraint dno_pk primary key,
dnamevarchar(10),
cityvarchar(10)
)
insert into department
values(10,'sales','hyderabad')
insert into department
values(20,'IT','Banglore')
select * from department
create table emp10
(
enointconstraint eno_primaryk primary key,
enamevarchar(10),
salint,
deptidintreferences department(dno)
)
insert into emp10 values(100,'srinivas',50000,30)’in valid
insert into emp10 values(100,'srinivas',50000,20)’valid
insert into emp10
values(100,'srinivas',50000,10)’invalid
select * from emp10
all the below insertions are
valid
insert into emp10 values(101,'satish',40000,10)
insert into emp10
values(102,'suresh',40000,20)
insert into emp10
values(103,'ramesh',40000,10)
select * from department
select * from emp10
delete from emp10
whereeno=103‘will delete the record from child table
delete from department
wheredno=20’it won’t delete the record from the parent table as it has
dependent records
If we want to delete/update the records
from parent table it is not possible because it is having dependency with child
table.
We need to use cascade delete and update to reflect the
changes with respect to parent table
On delete cascade:
If a parent record is deleted
from parent table all the related child records will be deleted from child
table.
On update cascade:
If a parent row is updated in
parent table then te related child rows will be updated in child table.
create table department1
(
dnointconstraint dno_pk primary key,
dnamevarchar(10),
cityvarchar(10)
)
Insert into
department1values(10,'sales','hyderabad')
Insert into
department1values(20,'IT','Banglore')
create table emp10
(
enointconstraint eno_primaryk primary key,
enamevarchar(10),
salint,
deptidintreferences department(dno) on delete cascade)
delete department1 where dno=10 ‘all the emp10 records from child table will be deleted.
Table level constraint:
create table emp12
(
enoint,
enamevarchar(10),
constraint tableunique(eno,ename)
)
insert into emp12
values(100,'srinivas')
100 srinivas
100 satish
The above tow are different
entries.
create table emp13
(
enoint,
enamevarchar(10),
constraint tableleve13 primary
key(eno,ename)
)
insert into emp13
values(100,'srinivas') ‘valid
insert into emp13
values(101,'srinivas')’valid
insert into emp13
values(101,'srinivas')’invalid
Composite key:
If primary key is given on two
columns in the table then it is called composite primary key. Composite means
mixed data (integers ,characters,and float values)
Let me know if there are any doubts on this article.
Thanks
Srinivas