Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
32
Добавлен:
16.04.2013
Размер:
4.96 Mб
Скачать

Enforcing Referential Integrity with Constraints

Although you can declare constraints on views, such constraints do not help maintain data integrity. Instead, they are used to enable query rewrites on queries involving views, which helps performance with materialized views and other data warehousing features. Such constraints are always declared with the DISABLE keyword, and you cannot use the VALIDATE keyword. The constraints are never enforced, and there is no associated index.

See Also: Oracle Data Warehousing Guide for information on query rewrite, materialized views, and the performance reasons for declaring constraints on views

Enforcing Referential Integrity with Constraints

Whenever two tables contain one or more common columns, Oracle Database can enforce the relationship between the two tables through a referential integrity constraint. Define a PRIMARY or UNIQUE key constraint on the column in the parent table (the one that has the complete set of column values). Define a FOREIGN KEY constraint on the column in the child table (the one whose values must refer to existing values in the other table).

See Also: "Defining Relationships Between Parent and Child Tables" on page 3-10 for information on defining additional integrity constraints, including the foreign key

Figure 3–3 shows a foreign key defined on the department number. It guarantees that every value in this column must match a value in the primary key of the department table. This constraint prevents erroneous department numbers from getting into the employee table.

Foreign keys can be comprised of multiple columns. Such a composite foreign key must reference a composite primary or unique key of the exact same structure, with the same number of columns and the same datatypes. Because composite primary and unique keys are limited to 32 columns, a composite foreign key is also limited to 32 columns.

3-8 Oracle Database Application Developer's Guide - Fundamentals

Enforcing Referential Integrity with Constraints

Figure 3–3 Tables with Referential Integrity Constraints

Parent Key

Primary key of referenced table

Table DEPARTMENTS

 

DEPID

DNAME

LOC

10

Administration

1700

20

Marketing

1800

30

Purchasing

1700

40

Human Resources

2400

Referenced or

Parent Table

Foreign Key

(values in dependent table must match a value in unique key or primary key of referenced table)

Table EMPLOYEES

 

 

ID

LNAME

JOB

MGR

 

HIREDATE

SAL

COMM

DEPTNO

 

 

100

King

AD_PRES

 

 

17–JUN–87

24000

 

90

 

 

 

 

101

Kochhar

AD_VP

100

 

21–SEP–89

17000

 

90

 

 

 

 

102

De Hann

AD_VP

100

 

13–JAN–93

17000

 

90

 

 

 

 

103

Hunold

IT_PROG

102

 

03–JAN–90

9000

 

60

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Dependent or Child Table

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

INSERT

 

 

 

 

 

 

 

 

 

 

 

 

 

INTO

 

 

 

 

 

 

 

556

CRICKET

PU_CLERK

 

 

31–OCT–96

5000

 

25

 

 

 

 

 

 

 

 

 

 

 

556

CRICKET

PU_CLERK

 

 

31–OCT–96

5000

 

 

 

 

 

 

 

 

 

 

 

 

 

 

This row violates the referential constraint because "50"

is not present

in the referenced table's primary key; therefore, the row is not allowed in

the table.

This row is allowed in the table because a null value is entered in the DEPTNO column; however, if a not null constraint is also defined for this column, this row is not allowed.

Maintaining Data Integrity Through Constraints 3-9

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

Соседние файлы в папке Oracle 10g