You are on page 1of 14

Integrity Constraints

Data integrity allows to define certain data quality requirements that the
data in the database needs to meet. If a user tries to insert data that
doesn't meet these requirements, Oracle will not allow so.
Constraint types
There are five integrity constraints in Oracle.

1- Not Null

2-Unique Key

3-Primary Key

4-Foreign Key

5-Check
Why do we need to use constraints ?

ID NAME AGE ID NAME GPA


----------------------------
--------------------------------------------
1 AYNUR 20
1 AYNUR A
2 ASEL 250
2 ASEL Z
2 NURLAN 30
3 NURLAN B+

250 can be inserted,


But “age” can not be 250.

ID must be unique,
id=2 is already used. There is no grade as “Z”

You may use constraints to avoid these logical errors.


parent child

ID NAME ID GPA
-------------------------- --------------------
1 NURLAN 1 A+
2 ALIYA 2 B
3 SAMAT 3 A
4 ASEL 4 C
6 D

6 can be inserted but,


there is no student whose id=6.
This is a logical error.

To avoid this,
parent.id must be primary key
child.id must be foreign key and referenced by parent.id
1- Not Null
A column in a table can be specified not null. It's not possible to insert a null
in such a column. The default is null. So, in the following create table
statement, a null can be inserted into the column named c.

create table MyNumbers1 (


a number not null,
b number null,
c number
);

SQL> insert into MyNumbers1 values ( 1, null, null);


SQL> insert into MyNumbers1 values ( 2, 3, 4);
SQL> insert into MyNumbers1 values (null, 5, 6); >>>>error

The first two records can be inserted but,


The third cannot, throwing an error
2-Unique Key
The unique constraint doesn't allow duplicate values in a column. If the unique
constraint encompasses two or more columns, no two equal combinations are
allowed.
create table MyNumbers2 (
a number unique, However, if a column is not defined as not null,
b number nulls can be inserted multiple times:
);
SQL> insert into MyNumbers2 values (4, 5);
SQL> insert into MyNumbers2 values (2, 1);
SQL> insert into MyNumbers2 values (9, 8);
SQL> insert into MyNumbers2 values (6, 9);
SQL> insert into MyNumbers2 values (null,9);
SQL> insert into MyNumbers2 values (null,9);

Now: trying to insert the number 2 again into a:


SQL> insert into MyNumbers2 values (2,7);

This statement issues a ORA-00001: unique constraint (SCOTT.SYS_C001463


violated). Every constraint, by the way, has a name. In this case, the name is:
SCOTT.SYS_C001463.
A unique constraint can be extended over multiple columns:

create table MyNumbers3 (


a number,
b number,
c number,
unique (a,b)
);

It is possible to name the constraint. The following example creates a unique


constraint on the columns a and b and names the constraint ab_unique.

create table Mynumbers4 (


a number,
b number,
c number,
constraint ab_unique unique (a,b)
);
3-Primary Key
On a technical level, a primary key combines a unique and a not null constraint.

create table MyNumbers5 (


a number primary key, Primary key name is
b number given by oracle
);

Primary keys can explicitely be named.


The following create table statement creates a table with a primary key whose name is
pk_name.

create table Mynumbers6 ( Primary key name is


a number, given by user
b number,
c number,
constraint pk_name primary key (a, b)
);
4-Foreign Key
A foreign key means that values in one table must also appear in another table.

The referenced table is called the parent table while the table with the foreign key is called the child
table. The foreign key in the child table will generally reference a primary key in the parent table.
Parent table Child Table
Table Name : A Table Name : B
CODE_A NAME CODE_B ENT
------------------------ ---------------------------
1 AYNUR 1 102
2 MARAT
3 SAMAT

Create table A ( Create table B (


code_a number primary key, code_b number ,ent number,
name varchar2(10) constraint FK_code foreign key (code_b)
); References A(code_a)
);
All rows can be inserted to A Any row can be deleted from B
Any row can not be inserted to B if it is not
It can not be deleted if it is exist in B.
exist in A.
5-Check
A check constraint allows to state a minimum requirement for the value in a
column.
It allows only numbers that are between 0 and
100 in the column a;

create table MyNumbers7 (


a number check (a between 0 and 100),
b number
);

The following table allows It allows to inserted A,B,C,D and F into ENT
to state a<b .
create table MyEnt ( id number, Ent varchar2(1)
create table MyNumbers8 ( check (ent in ('A','B','C','D','F'))
a number, );
b number,
check (a<b),
);
Adding, Dropping, Enable and disable Constraints

Create a table
SQL> Create Table MyNumbers9 (a number, b number);
Add constraint
SQL >Alter Table MyNumbers9 Add Constraint MY_C1 check (a<10) ;
Disable Constraint
SQL> Alter Table MyNumbers9 disable constraint MY_C1 ;
Enable Constraint
SQL> Alter Table MyNumbers9 enable constraint MY_C1 ;
Drop constraint
SQL> Alter Table MyNumbers9 drop constraint MY_C1;

It shows all constraints in MyNumbers table.


SQL> Select * from all_constraints where table_name=‘MyNumbers9’ ;
NOW,
IT IS YOUR TURN.

You might also like