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

Oracle 约束的基础知识介绍

[日期:2012-10-17] 来源:Linux社区  作者:luoping [字体:大 中 小]

3,约束的维护

  1. 3.1 约束更改名字 
  2.  
  3. alter table test_cons rename constraint cons_id to cons_1_id; 
  4.  
  5. 3.2 启用与禁用约束 
  6.  
  7. alter table test_cons disable constraint cons_1_id; 
  8.  
  9. alter table test_cons disable constraint cons_1_id keep index; 
  10.  
  11. alter table test_cons disable primary key cascade; 
  12.  
  13. alter table test_cons enable constraint cons_1_id; 
  14.  
  15. alter table test_cons enable novalidate constraint cons_1_id; 
  16.  
  17. alter table test_cons enable  validate constraint cons_1_id; 
  18.  
  19. 3.3  修改与删除约束 
  20.  
  21. alter table test_cons modify constraint cons_1_id novalidate; 
  22.  
  23. alter table test_cons modify constraint cons_1_id validate; 
  24.  
  25. alter table test_cons drop constraint cons_1_id keep index; 
  26.  
  27. alter table test_cons drop constraint cons_1_id; 

4,约束的Exception

If exceptions exist when a constraint is validated, an error is returned and the integrity constraint remains novalidated. When a statement is not successfully executed because integrity constraint exceptions exist, the statement is rolled back. If exceptions exist, you cannot validate the constraint until all exceptions to the constraint are either updated or deleted.

You must create an appropriate exceptions report table to accept information from the EXCEPTIONS option of the ENABLE clause before enabling the constraint. You can create an exception table by executing the UTLEXCPT.SQL script or the UTLEXPT1.SQL script.

Both of these scripts create a table named EXCEPTIONS. You can create additional exceptions tables with different names by modifying and resubmitting the script.

The following statement attempts to validate the PRIMARY KEY of the dept table, and if exceptions exist, information is inserted into a table named EXCEPTIONS:

ALTER TABLE dept ENABLE PRIMARY KEY EXCEPTIONS INTO EXCEPTIONS;

SELECT * FROM EXCEPTIONS;

The following exceptions are shown:

fROWID              OWNER      TABLE_NAME      CONSTRAINT

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

AAAAZ9AABAAABvqAAB  SCOTT      DEPT            SYS_C00610

AAAAZ9AABAAABvqAAG  SCOTT      DEPT            SYS_C00610

SELECT deptno, dname, loc FROM dept, EXCEPTIONS

    WHERE EXCEPTIONS.constraint = 'SYS_C00610'

    AND dept.rowid = EXCEPTIONS.row_id;

5,约束的Defer

When the database checks a constraint, it signals an error if the constraint is not satisfied. You can defer checking the validity of constraints until the end of a transaction.

When you issue the SET CONSTRAINTS statement, the SET CONSTRAINTS mode lasts for the duration of the transaction, or until another SET CONSTRAINTS statement resets the mode.

Set All Constraints Deferred

Within the application being used to manipulate the data, you must set all constraints deferred before you actually begin processing any data. Use the following DML statement to set all deferrable constraints deferred:

SET CONSTRAINTS ALL DEFERRED;

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 fails and the constraint causing the error is identified. If you commit while constraints are violated, the transaction is rolled back and you receive an error message

linux
相关资讯       Oracle基础知识  Oracle约束 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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