你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle约束知识笔记

[日期:2012-08-19] 来源:Linux社区  作者:indexman [字体:大 中 小]

2. 使用异常表

  1. SQL> alter table dept2 modify constraint dept2_u1 disable novalidate;  
  2.   
  3. 表已更改。  
  4.   
  5. SQL> insert into dept2 (deptno) values(10);  
  6.   
  7. 已创建 1 行。  
  8.   
  9. SQL> alter table dept2  
  10.   2  enable validate constraint dept2_u1  
  11.   3  exceptions into exceptions;  
  12. alter table dept2  
  13. *  
  14. 第 1 行出现错误:  
  15. ORA-02299: 无法验证 (SCOTT.DEPT2_U1) - 找到重复关键字  

3. 使用子查询查找异常表记录的非法记录:
  1. SQL> select rowid,dept2.* from dept2  
  2.   2  where rowid in (select row_id from exceptions) for update;  
  3.   
  4. ROWID                  DEPTNO DNAME          LOC  
  5. ------------------ ---------- -------------- -------------   
  6. AAAMlpAAEAAAAHkAAA         10 ACCOUNTING     NEW YORK  
  7. AAAMlpAAEAAAAHlAAB         10  
  8.   
  9. SQL> update dept2  
  10.   2  set deptno = 50  
  11.   3  where rowid='AAAMlpAAEAAAAHlAAB';  
  12.   
  13. 已更新 1 行。  
  14.   
  15. SQL> commit;  
  16.   
  17. 提交完成。  
4. How to Identify Row Violation (continued)
  1. 5.  Truncate the EXCEPTIONS table and reenable the constraint:  
  2. SQL> TRUNCATE TABLE exceptions;  
  3. Statement processed.  
  4. SQL> ALTER TABLE hr.employees  
  5.   2  ENABLE VALIDATE CONSTRAINT employee_dept_id_fk  
  6.   3  EXCEPTIONS INTO system.exceptions;  
  7. Statement processed.  
八   从数据字典获取约束信息

DBA_CONSTRAINTS
DBA_CONS_COLUMNS
  1. SQL> SELECT constraint_name, constraint_type, deferrable,  
  2.   2         deferred, validated  
  3.   3  FROM   <STRONG>dba_constraints</STRONG>  
  4.   4  WHERE  owner='HR'  
  5.   5  AND    table_name='EMPLOYEE';  
  6. CONSTRAINT_NAME   C     DEFERRABLE       DEFERRED    VALIDATED  
  7. ----------------  -     ---------------  -----------    ----------   
  8. EMPLOYEE_DEPT..   R     DEFERRABLE       DEFERRED    VALIDATED  
  9. EMPLOYEE_ID_PK    P     DEFERRABLE       IMMEDIATE    VALIDATED  
  10. SYS_C00565        C     NOT DEFERRABLE   IMMEDIATE    VALIDATED  
  11. 3 rows selected.  
要在HR下找到EMPLOYEE的外键以及关联的主键, 使用以下查询:
 
  1. SQL> SELECT c.constraint_name AS "Foreign Key",  
  2.   2         p.constraint_name AS "Referenced Key",  
  3.   3         p.constraint_type,  
  4.   4         p.owner,  
  5.   5         p.table_name  
  6.   6  FROM   dba_constraints c, dba_constraints p  
  7.   7  WHERE  c.owner='HR'  
  8.   8  AND    c.table_name='EMPLOYEE'  
  9.   9  AND    c.constraint_type='R'  
  10.   10 AND    c.r_owner=p.owner  
  11.   11 AND    c.r_constraint_name = p.constraint_name;  
  12. Foreign Key       Referenced Key  C  OWNER       TABLE_NAME      
  13. ------------      --------------  -  ----------  ----------   
  14. EMPLOYEES_DEPT..   DEPT_PK        P  HR          DEPARTMENT  
  15. 1 row selected.  
linux
相关资讯       Oracle约束 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款