2. 使用异常表
- SQL> alter table dept2 modify constraint dept2_u1 disable novalidate;
- 表已更改。
- SQL> insert into dept2 (deptno) values(10);
- 已创建 1 行。
- SQL> alter table dept2
- 2 enable validate constraint dept2_u1
- 3 exceptions into exceptions;
- alter table dept2
- *
- 第 1 行出现错误:
- ORA-02299: 无法验证 (SCOTT.DEPT2_U1) - 找到重复关键字
- SQL> select rowid,dept2.* from dept2
- 2 where rowid in (select row_id from exceptions) for update;
- ROWID DEPTNO DNAME LOC
- ------------------ ---------- -------------- -------------
- AAAMlpAAEAAAAHkAAA 10 ACCOUNTING NEW YORK
- AAAMlpAAEAAAAHlAAB 10
- SQL> update dept2
- 2 set deptno = 50
- 3 where rowid='AAAMlpAAEAAAAHlAAB';
- 已更新 1 行。
- SQL> commit;
- 提交完成。
- 5. Truncate the EXCEPTIONS table and reenable the constraint:
- SQL> TRUNCATE TABLE exceptions;
- Statement processed.
- SQL> ALTER TABLE hr.employees
- 2 ENABLE VALIDATE CONSTRAINT employee_dept_id_fk
- 3 EXCEPTIONS INTO system.exceptions;
- Statement processed.
DBA_CONSTRAINTS
DBA_CONS_COLUMNS
- SQL> SELECT constraint_name, constraint_type, deferrable,
- 2 deferred, validated
- 3 FROM <STRONG>dba_constraints</STRONG>
- 4 WHERE owner='HR'
- 5 AND table_name='EMPLOYEE';
- CONSTRAINT_NAME C DEFERRABLE DEFERRED VALIDATED
- ---------------- - --------------- ----------- ----------
- EMPLOYEE_DEPT.. R DEFERRABLE DEFERRED VALIDATED
- EMPLOYEE_ID_PK P DEFERRABLE IMMEDIATE VALIDATED
- SYS_C00565 C NOT DEFERRABLE IMMEDIATE VALIDATED
- 3 rows selected.
- SQL> SELECT c.constraint_name AS "Foreign Key",
- 2 p.constraint_name AS "Referenced Key",
- 3 p.constraint_type,
- 4 p.owner,
- 5 p.table_name
- 6 FROM dba_constraints c, dba_constraints p
- 7 WHERE c.owner='HR'
- 8 AND c.table_name='EMPLOYEE'
- 9 AND c.constraint_type='R'
- 10 AND c.r_owner=p.owner
- 11 AND c.r_constraint_name = p.constraint_name;
- Foreign Key Referenced Key C OWNER TABLE_NAME
- ------------ -------------- - ---------- ----------
- EMPLOYEES_DEPT.. DEPT_PK P HR DEPARTMENT
- 1 row selected.
