Professional Documents
Culture Documents
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 must be unique,
id=2 is already used. There is no grade as “Z”
ID NAME ID GPA
-------------------------- --------------------
1 NURLAN 1 A+
2 ALIYA 2 B
3 SAMAT 3 A
4 ASEL 4 C
6 D
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.
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
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;