Вы находитесь на странице: 1из 44

Introduction to Data Integrity

Integrity constraint.

An integrity constraint is a rule that restricts the values for one or


more columns in a table.
Types of Data Integrity

NULL specifies that a column can contain null values.

NOT NULL specifies that a column cannot contain null values.

If you do not specify NULL or NOT NULL in a column definition,


NULL is the default.

UNIQUE designates a column or combination of columns as


a unique key.
PRIMARY KEY designates a column or combination
of columns as the table's primary key.

FOREIGN KEY designates a column or combination


of columns as the foreign key in a referential integrity
constraint.

CHECK specifies a condition that each row in the table


must satisfy.
Database Triggers

Oracle also lets you enforce integrity rules with a non-


declarative approach using database triggers (stored database
procedures automatically invoked on insert, update, or delete
operations).

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

To define an integrity constraint, include a CONSTRAINT


clause in CREATE TABLE or ALTER TABLE statement.

The CONSTRAINT clause has two syntactic forms:


Table_constraint

The table_constraint syntax is part of the table


definition.
An integrity constraint defined with this syntax can impose
rules on any columns in the table.

The table_constraint syntax can appear in a CREATE


TABLE or ALTER TABLE statement.

This syntax can define any type of integrity constraint except


a NOT NULL constraint.
column_constraint

The column_constraint syntax is part of a column


definition.
Usually, an integrity constraint defined with this syntax can only
impose rules on the column in which it is defined.

The table_constraint syntax and the column_constraint syntax are


simply different syntactic means of defining integrity constraints.

A constraint that references more than one column must be defined


as a table constraint.
NOT NULL Constraints

The NOT NULL constraint specifies that a column cannot


contain nulls.
To satisfy this constraint, every row in the table must contain a
value for the column.

The NULL keyword indicates that a column can contain nulls. It


does not actually define an integrity constraint. If you do not
specify either NOT NULL or NULL, the column can contain nulls
by default.

You can only specify NOT NULL or NULL with


column_constraint syntax in a CREATE TABLE or ALTER
TABLE statement, not with table_constraint syntax.
Example I

The following statement alters the EMP table and defines and
enables a NOT NULL constraint on the SAL column:

ALTER TABLE emp


MODIFY (sal NUMBER CONSTRAINT nn_sal NOT
NULL)

NN_SAL ensures that no employee in the table has a null salary.


UNIQUE Constraints

The UNIQUE constraint designates a column or combination of


columns as a unique key.
To satisfy a UNIQUE constraint, no two rows in the table can have
the same value for the unique key.

However, the unique key made up of a single column can contain


nulls.
A unique key column cannot be of datatype LONG or LONG RAW.

You cannot designate the same column or combination of columns


as both a unique key and a primary key

However, you can designate the same column or combination of


columns as both a unique key and a foreign key.
Defining Unique Keys

Example II
CREATE TABLE dept
(deptno NUMBER(2),
dname VARCHAR2(9) CONSTRAINT unq_dname
UNIQUE,
loc VARCHAR2(10) )

The constraint UNQ_DNAME identifies the DNAME column as a


unique key. This constraint ensures that no two departments in the
table have the same name.

However, the constraint does allow departments without names.


Alternatively, you can define and enable this constraint with the
table_constraint syntax:

CREATE TABLE dept


(deptno NUMBER(2),
dname VARCHAR2(9),
loc VARCHAR2(10),
CONSTRAINT unq_dname UNIQUE (dname) )
Defining Composite Unique Keys

A composite unique key is a unique key made up of a combination


of columns. To define a composite unique key, you must use
table_constraint syntax, rather than column_constraint syntax.

To satisfy a constraint that designates a composite unique key, no


two rows in the table can have the same combination of values in
the key columns.

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:

ALTER TABLE census


ADD CONSTRAINT unq_city_state
UNIQUE (city, state) ;

The UNQ_CITY_STATE constraint ensures that the


same combination of CITY and STATE values does
not appear in the table more than once.
PRIMARY KEY Constraints

A PRIMARY KEY constraint designates a column or combination


of columns as the table's primary key. To satisfy a PRIMARY
KEY constraint, both of the following conditions must be true:

No primary key value can appear in more than one row in the
table.

No column that is part of the primary key can contain a null.


A table can have only one primary key.
A primary key column cannot be of datatype LONG or LONG
RAW.
Defining Primary Keys

Example IV

CREATE TABLE dept


(deptno NUMBER(2) CONSTRAINT pk_dept PRIMARY
KEY, dname VARCHAR2(9), loc VARCHAR2(10) )

The PK_DEPT constraint identifies the DEPTNO column as the


primary key of the DEPTNO table.

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:

CREATE TABLE dept


(deptno NUMBER(2),
dname VARCHAR2(9),
loc VARCHAR2(10),
CONSTRAINT pk_dept PRIMARY KEY
(deptno) )
Defining Composite Primary Keys

A composite primary key is a primary key made up of a


combination of columns..

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:

ALTER TABLE ship_cont


ADD PRIMARY KEY (ship_no, container_no)
Referential Integrity Constraints

A referential integrity constraint designates a column or


combination of columns as a foreign key and establishes a
relationship between that foreign key and a specified primary or
unique key, called the referenced key.

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

You can use column_constraint syntax to define a referential


integrity constraint in which the foreign key is made up of a
single column.
Example VI
The following statement creates the EMP table and defines
and enables a foreign key on the DEPTNO column that
references the primary key on the DEPTNO column of the
DEPT table:
CREATE TABLE emp
(empno NUMBER(4), ename VARCHAR2(10),
job VARCHAR2(9), mgr NUMBER(4),
hiredate DATE, sal NUMBER(7,2),
comm NUMBER(7,2), deptno number (4)
CONSTRAINT fk_deptno REFERENCES dept(deptno) )

The constraint FK_DEPTNO ensures that all employees in the EMP


table work in a department in the DEPT table. However, employees
can have null department numbers.

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.

Note that the constraint definition identifies both the parent


table and the columns of the referenced key. Because the
referenced key is the parent table's primary key, the referenced
key column names are optional.

Note that the above statement omits the DEPTNO column's


datatype. Because this column is a foreign key, Oracle7
automatically assigns it the datatype of the DEPT.DEPTNO
column to which the foreign key refers.
Alternatively, you can define a referential integrity
constraint with table_constraint syntax:

CREATE TABLE emp (empno NUMBER(4),


Ename VARCHAR2(10),
Job VARCHAR2(9),
Mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER ,
CONSTRAINT fk_deptno FOREIGN KEY
(deptno) REFERENCES dept(deptno) )
Maintaining Referential Integrity with the ON DELETE
CASCADE Option

If you use the ON DELETE CASCADE option, Oracle7 permits


deletions of referenced key values in the parent table and
automatically deletes dependent rows in the child table to
maintain referential integrity.
Example VII
This example creates the EMP table, defines and enables the
referential integrity constraint FK_DEPTNO, and uses the ON
DELETE CASCADE option
CREATE TABLE emp
(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)

CONSTRAINT fk_deptno REFERENCES dept(deptno)


ON DELETE CASCADE )
Because of the ON DELETE CASCADE option, Oracle
cascades any deletion of a DEPTNO value in the DEPT table
to the DEPTNO values of its dependent rows of the EMP
table. For example, if department 20 is deleted from the DEPT
table, Oracle deletes the department's employees from the
EMP table.
CHECK Constraints

The CHECK constraint explicitly defines a condition. To satisfy the


constraint, each row in the table must make the condition either TRUE
or unknown (due to a null). For information on conditions, see the
syntax description of condition. The condition of a CHECK constraint
can refer to any column in the table, but it cannot refer to columns of
other tables.

Whenever Oracle7 evaluates a CHECK constraint condition for a


particular row, any column names in the condition refer to the column
values in that row.

If you create multiple CHECK constraints for a column, design them


carefully so their purposes do not conflict. Oracle7 does not verify that
CHECK conditions are not mutually exclusive.
Example IX
The following statement creates the DEPT table and defines a
CHECK constraint in each of the table's columns:

CREATE TABLE dept (deptno NUMBER CONSTRAINT


check_deptno CHECK (deptno BETWEEN 10 AND 99) ,
dname VARCHAR2(9) CONSTRAINT check_dname
CHECK (dname = UPPER(dname))
,
loc VARCHAR2(10) CONSTRAINT check_loc CHECK (loc IN
('DALLAS','BOSTON','NEW YORK','CHICAGO')) )
Example X
The following statement creates the EMP table and uses a
table constraint clause to define and enable a CHECK
constraint:

CREATE TABLE emp

(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:

CREATE TABLE order_detail (


CONSTRAINT pk_od PRIMARY KEY (order_id,
part_no), order_id NUMBER CONSTRAINT fk_oid
REFERENCES scott.order (order_id),
part_no NUMBER CONSTRAINT fk_pno
REFERENCES scott.part (part_no),
quantity NUMBER CONSTRAINT nn_qty NOT
NULL
CONSTRAINT check_qty_low
CHECK (quantity > 0), cost NUMBER CONSTRAINT
check_cost CHECK (cost > 0) )
A table can have multiple CHECK constraints. Multiple
CHECK constraints, each with a simple condition
enforcing a single business rule is better than a single
CHECK constraint with a complicated condition enforcing
multiple business rules. When a constraint is violated,
Oracle7 returns an error message identifying the
constraint. Such an error message more precisely identifies
the violated business rule if the identified constraint
enforces a single business rule.
CREATE TABLE emp
(empno NUMBER CONSTRAINT pk_emp
PRIMARY KEY,
ename VARCHAR2(10) CONSTRAINT nn_ename
NOT NULL
CONSTRAINT upper_ename
CHECK (ename = UPPER(ename)),
job VARCHAR2(9),
mgr NUMBER CONSTRAINT fk_mgr
REFERENCES emp(empno), hiredate DATE DEFAULT
SYSDATE, sal NUMBER(10,2)
CONSTRAINT ck_sal CHECK (sal > 500),
comm NUMBER(9,0) DEFAULT NULL,
deptno NUMBER(2) CONSTRAINT nn_deptno NOT
NULL CONSTRAINT fk_deptno REFERENCES dept(deptno) );
CREATE TABLE Dept_tab
( Deptno NUMBER(3)
CONSTRAINT Dept_pkey PRIMARY KEY,
Dname VARCHAR2(15),
Loc VARCHAR2(15),
CONSTRAINT Dname_ukey UNIQUE (Dname, Loc),
CONSTRAINT Loc_check1 CHECK (loc IN ('NEW YORK',
'BOSTON', 'CHICAGO')));
CREATE TABLE Emp_tab (
Empno NUMBER(5) CONSTRAINT Emp_pkey
PRIMARY KEY,
Ename VARCHAR2(15) NOT NULL,
Job VARCHAR2(10),
Mgr NUMBER(5) CONSTRAINT Mgr_fkey
REFERENCES Emp_tab,
Hiredate DATE,
Sal NUMBER(7,2),
Comm NUMBER(5,2),
Deptno NUMBER(3) NOT NULL
CONSTRAINT dept_fkey REFERENCES Dept_tab ON
DELETE CASCADE);