zoukankan      html  css  js  c++  java
  • 约束的DEFERRABLE, NOT DEFERRABLE, INITIALLY IMMEDIATE 和 INITIALLY DEFERRED

    [ CONSTRAINT constraint_name ]
    { NOT NULL |
      NULL |
      CHECK ( expression ) [ NO INHERIT ] |
      DEFAULT default_expr |
      UNIQUE index_parameters |
      PRIMARY KEY index_parameters |
      REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
        [ ON DELETE action ] [ ON UPDATE action ] }
    [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]

     

    DEFERRABLE(可以推迟的约束检查):可以推迟到事务结尾。目前仅UNIQUE, PRIMARY KEY, EXCLUDE和FOREIGN KEY才支持DEFERRABLE。NOT NULL和CHECK约束不支持。

    NOT DEFERRABLE(不可推迟的约束检查):在每一个命令后会立即验证是否满足约束条件。缺省是NOT DEFERRABLE。

    如果约束是INITIALLY IMMEDIATE,那么每条语句之后就检查它。这个是缺省。

    如果约束是INITIALLY DEFERRED,那么直到事务完成才检查。 

     

    检查的时间可以用以下命令修改:

    SET CONSTRAINTS [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]

     

    例子:

    • 默认是NOT DEFERRABLE INITIALLY IMMEDIATE

    create table t1 (id number, name char(10));

    alter table t1 modify id number primary key;

    SQL> insert into t1 values(1,'Tough1');
    已创建 1 行。

    SQL> insert into t1 values(1,'Tough1');
    insert into t1 values(1,'Tough1')
    *
    第 1 行出现错误:
    ORA-00001: 违反唯一约束条件 (SCOTT.SYS_C005428)

     

    • DEFERRABLE或INITIALLY DEFERRED的情况:

    create table t2 (id number, name char(10));

    alter table t2 modify id number primary key INITIALLY DEFERRED;

    alter table t2 modify id number primary key DEFERRABLE INITIALLY DEFERRED;

    SQL> insert into t2 values(1,'Tough1');

    已创建 1 行。

    SQL> insert into t2 values(1,'Tough1');

    已创建 1 行。

    SQL> commit;
    commit
    *
    第 1 行出现错误:
    ORA-02091: 事务处理已回退
    ORA-00001: 违反唯一约束条件 (SCOTT.SYS_C005435)

     


     

     

  • 相关阅读:
    素数判定方法,方法持续更新...
    C++暂停黑窗口
    【python爬虫】scrapy入门6-生成多个spider
    【python爬虫】scrapy入门5--xpath等后面接正则
    python基础:list与string互转
    【python爬虫】scrapy实战1--百万微博任性采集
    【python爬虫】scrapy入门4--添加cookies
    【python爬虫】scrapy入门2--自定义item
    【python爬虫】scrapy入门1--环境搭建
    sklearn学习:为什么roc_auc_score()和auc()有不同的结果?
  • 原文地址:https://www.cnblogs.com/toughhou/p/3778798.html
Copyright © 2011-2022 走看看