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

Enforcing Referential Integrity with Constraints

Deferring Constraint Checks

When Oracle Database checks a constraint, it signals an error if the constraint is not satisfied. You can use the SET CONSTRAINTS statement to defer checking the validity of constraints until the end of a transaction.

Note: You cannot issue a SET CONSTRAINT statement inside a trigger.

The SET CONSTRAINTS setting lasts for the duration of the transaction, or until another SET CONSTRAINTS statement resets the mode.

See Also: Oracle Database SQL Reference for more details on SET

CONSTRAINTS

Guidelines for Deferring Constraint Checks

Select Appropriate Data You may wish to defer constraint checks on UNIQUE and FOREIGN keys if the data you are working with has any of the following characteristics:

Tables are snapshots.

Some tables contain a large amount of data being manipulated by another application, which may or may not return the data in the same order.

Update cascade operations on foreign keys.

Ensure Constraints Are Created Deferrable After you have identified and selected the appropriate tables, make sure their FOREIGN, UNIQUE and PRIMARY key constraints are created deferrable. You can do so by issuing statements similar to the following:

CREATE TABLE dept (

deptno NUMBER PRIMARY KEY, dname VARCHAR2 (30)

);

CREATE TABLE emp ( empno NUMBER,

ename VARCHAR2 (30),

deptno NUMBER REFERENCES (dept),

CONSTRAINT epk PRIMARY KEY (empno) DEFERRABLE,

CONSTRAINT efk FOREIGN KEY (deptno)

REFERENCES (dept.deptno) DEFERRABLE);

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

Enforcing Referential Integrity with Constraints

INSERT INTO dept VALUES (10, 'Accounting');

INSERT INTO dept VALUES (20, 'SALES');

INSERT INTO emp VALUES (1, 'Corleone', 10);

INSERT INTO emp VALUES (2, 'Costanza', 20);

COMMIT;

SET CONSTRAINT efk DEFERRED;

UPDATE dept SET deptno = deptno + 10

WHERE deptno = 20;

SELECT * from

emp ORDER BY deptno;

EMPNO ENAME

DEPTNO

------------------- -------

1

Corleone

10

2

Costanza

20

UPDATE emp SET deptno = deptno + 10

WHERE deptno

= 20;

SELECT * FROM emp

ORDER BY deptno;

EMPNO ENAME

DEPTNO

------------------- -------

1

Corleone

10

2

Costanza

30

COMMIT;

Set All Constraints Deferred Within the application that manipulates the data, you must set all constraints deferred before you begin processing any data. Use the following DML statement to set all constraints deferred:

SET CONSTRAINTS ALL DEFERRED;

Note: The SET CONSTRAINTS statement applies only to the current transaction. The defaults specified when you create a constraint remain as long as the constraint exists. The ALTER SESSION SET CONSTRAINTS statement applies for the current session only.

Check the Commit (Optional) You can check for constraint violations before committing by issuing the SET CONSTRAINTS ALL IMMEDIATE statement just before issuing the COMMIT. If there are any problems with a constraint, this statement will fail and the constraint causing the error will be identified. If you commit while constraints are violated, the transaction will be rolled back and you will receive an error message.

Maintaining Data Integrity Through Constraints 3-13

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