Beruflich Dokumente
Kultur Dokumente
Integrity constraint.
Note:
You cannot enforce referential integrity using declarative
integrity constraints if child and parent tables are on different
nodes of a distributed database.
However, you can enforce referential integrity in a
distributed database using database triggers
Introduction to Integrity Constraints
• Oracle uses integrity constraints to prevent invalid data entry into the
base tables of the database.
• You can define integrity constraints to enforce the business rules
• If any of the results of a DML statement execution violate an
integrity constraint, then Oracle rolls back the statement and returns an
error.
Example:
Assume that you define an integrity constraint for the salary
column of the employees table.
This integrity constraint enforces the rule that no row in this table can
contain a numeric value greater than 10,000 in this column.
If an INSERT or UPDATE statement attempts to violate this integrity
constraint, then Oracle rolls back the statement and returns an
information error message.
Defining Integrity Constraints
The following statement alters the EMP table and defines and
enables a NOT NULL constraint on the SAL column:
Example II
CREATE TABLE dept
(deptno NUMBER(2),
dname VARCHAR2(9) CONSTRAINT unq_dname
UNIQUE,
loc VARCHAR2(10) )
Also, any row that contains nulls in all key columns automatically
satisfies the constraint.
However, two rows that contain nulls for one or more key columns
and the same combination of values for the other key columns
violate the constraint.
Example III
The following statement defines and enables a
composite unique key on the combination of the CITY
and STATE columns of the CENSUS table:
No primary key value can appear in more than one row in the
table.
Example IV
This constraint ensures that no two departments in the table have the
same department number and that no department number is NULL.
Alternatively, you can define and enable this constraint
with table_constraint syntax:
Example V
The following statement defines a composite primary key on the
combination of the SHIP_NO and CONTAINER_NO columns of
the SHIP_CONT table:
In this relationship, the table containing the foreign key is called the
child table and the table containing the referenced key is called the
parent table.
.
Self-Referential Integrity Constraints
The foreign key and the referenced key can be in the same
table.
In this case, the parent and child tables are the same.
To satisfy a referential integrity constraint, each row of the
child table must meet one of the following conditions:
The value of one of the columns that makes up the foreign key
must be null.
Defining Referential Integrity Constraints
Before you define and enable this constraint, you must define and
enable a constraint that designates the DEPTNO column of the
DEPT table as a primary or unique key.
Note that the referential integrity constraint definition does not
use the FOREIGN KEY keyword to identify the columns that
make up the foreign key. Because the constraint is defined
with a column constraint clause on the DEPTNO column, the
foreign key is automatically on the DEPTNO column.
(empno NUMBER(4),
ename VARCHAR2(10),
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2) ), CHECK
(sal + comm <= 5000) )
Example XI
The following statement defines and enables a PRIMARY KEY
constraint, two referential integrity constraints, a NOT NULL
constraint, and two CHECK constraints: