10.1 、Oracle 约束的DEFERRABLE, NOT DEFERRABLE, INITIALLY IMMEDIATE 和 INITIALLY DEFERRED
- 约束的 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 IMMEDIATEcreate 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 或I NITIALLY 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)
5种约束:NOT NULL 非空 NNUNIQUE 唯一 UKPRIMARY KEY 主键 PKFOREIGN KEY 外键 FKCHECK 条件 CK约束创建:1) 创建对象时定义2) 创建对象后定义3) 约束有名字:默认:SYS_C0, 系统推荐:表名_列名_约束类型create table scott.tabdrop table scott.me_stu purge;create table scott.me_stu(id varchar2(20) constraints stu_id_pk primary key,nam varchar2(32) constraints stu_nam_uk unique,age number(10) constraints stu_sal_ck check(age>0),notes varchar2(32) constraints stu_not_nn not null) compress nologging;或drop table scott.me_stu purge;create table scott.me_stu(id varchar2(20),nam varchar2(32),age number(10) ,notes varchar2(64),constraints stu_id_pk primary key(id),constraints stu_nam_uk unique(nam),constraints stu_sal_ck check(age>0),constraints stu_notes_nn check(notes is not null)) compress nologging;alter table scott.me_stu move compress;--外键
--参照完整性约束
--限制(Restrict)。不允许进行修改或删除操作。若修改或删除主表的主键时,如果子表中存在子记录,系统将产生一个错误提示。这是缺省的参照完整性设置。
--置空(Set Null)。如果外键列允许为空,若修改或删除主表的主键时,把子表中参照的外键列设置为空值(NULL)。
--置为缺省(Set Default)。如果指定了缺省值,若修改或删除主表的主键时,把子表中参照的外键设置为缺省值(Default)。
--级联(Cascade)。把主表中主键修改为一个新的值时,相应修改子表中外键的值;或者删除主表中主键的记录时,要相应删除子表中外键的记录。drop table scott.me_score purge;create table scott.me_score (sid varchar2(20) constraint score_sid_fk references scott.me_stu(id),--constraint score_sid_fk references scott.me_stu(id) on delete cascade,cid varchar2(20),score number(10,2)) nologging;或drop table scott.me_score purge;create table scott.me_score (sid varchar2(20),cid varchar2(20),score number(10,2),--constraint score_sid_fk foreign key(sid) references scott.me_stu(id)constraint score_sid_fk foreign key(sid) references scott.me_stu(id) on delete cascade) nologging;--查询约束select * from dba_constraints where table_name like '%me_stu%';select * from dba_cons_columns where table_name like '%me_stu%';--删除约束alter table scott.me_stu drop constraints stu_nam_uk;alter table scott.me_score drop constraints score_sid_fk;--添加约束alter table scott.me_stu modify(nam varchar2(32) constraint stu_nam_nn unique);alter table scott.me_stu modify(notes varchar2(32) constraint stu_not_nn not null);alter table scott.me_stu add constraint stu_nam_uk unique(nam);alter table scott.me_score add constraint score_sid_fk foreign key(sid) references scott.me_stu(id) on delete cascade;--外键对DML影响
INSERT:1) 子表对父表无影响2) 插入子表外键时,父表必须存在DELETE:1)对子表DELETE操作对父表无影响2)对父表DELETE操作须先对子表删除UPDATE:1) 对父表和子表都有影响2) 对父表如果子表引用则不能更新3) 对子表UPDATE操作时,外键在父表必须存在--外键对DDL影响1) 对子表没影响,对父表如果子表引用则不能删除--为了消除影响可以在建立约束时添加子句:on delete set null 主表删除时,子表置空on delete cascade 主表删除时,子表级联删除--禁用与启用约束alter table scott.me_score disable constraint score_sid_fk;alter table scott.me_score enable constraint score_sid_fk;--约束(constraint)状态:ENABLED/DISABLEDVALIDATED/NOVALIDATEDDEFERRABLE/NON-DEFERRABLEDEFERRED/IMMEDIATERELY/NORELY1) DEFERRABLE/NON-DEFERRABLE,DEFERRED/IMMEDIATEdeferrable: constraint如果被定义成deferrable那么constraints可以在deferred和imediate两种状态相互转换not deferrable: constraint默认是not deferrable,同initially immediate,不能在deferred和imediate两种状态相互转换deferred: 意味着constraint将被延迟即在transaction过程中使constraint失效,等到如果transaction commit时transaction会变成immediateimmediate: 意味着constraint在transaction过程中使constraint一直有效deferrable initially immediate: 允许将constraint再改为initially deferreddeferrable initially deferred: 允许将constraint再改为initially immediatedrop table scott.test purge;create table scott.test( x number constraint check_x check (x > 0) deferrable initially immediate,y number constraint check_y check (y > 0) deferrable initially deferred)nologging;或alter table scott.test add constraint check_x (x > 0) deferrable initially immediate;alter table scott.test add constraint check_y (y > 0) deferrable initially deferred;insert into scott.test values ( 1,1 );commit;--initially immediate:在transaction过程中使constraint一直有效insert into scott.test values (-1,1 );ERROR at line 1:ORA-02290: check constraint (SCOTT.CHECK_X) violated--转换initially immediate=>deferredset constraint scott.check_x deferred;insert into scott.test values (-1,1 );1 row created.commit;ERROR at line 1:ORA-02091: transaction rolled backORA-02290: check constraint (SCOTT.CHECK_X) violated--initially deferred:constraint被延迟,transaction commit时transaction会变成immediateinsert into scott.test values ( 1,-1 );1 row created.commit;ERROR at line 1:ORA-02091: transaction rolled backORA-02290: check constraint (SCOTT.CHECK_Y) violated--转换initially deferred=>immediateset constraint scott.check_y immediate;insert into scott.test values ( 1,-1 );ERROR at line 1:ORA-02290: check constraint (SCOTT.CHECK_Y) violated1) enable/disable validate/novalidateenable/disable: 对未来的数据有约束/无约束validate/novalidate: 对已有的数据有约束/无约束启用约束:enable( validate) :启用约束,创建索引,对已有及新加入的数据执行约束.enable novalidate :启用约束,创建索引,仅对新加入的数据强制执行约束,而不管表中的现有数据.禁用约束:disable( novalidate):关闭约束,删除索引,可以对约束列的数据进行修改等操作.disable validate :关闭约束,删除索引,不能对表进行 插入/更新/删除等操作.注意:如果加约束到一个大表,那么ORACLE会LOCK这个表,然后SCAN所有数据,来判断是否符合CONSTRAINT的要求,在繁忙的系统里显然是不合适的。所以用enable、
novalidate比较合适,因为ORACLE仅仅会LOCK表一小段时间来建立CONSTRAINT,当CONSTRAINT建立后再VALIDATE,这时检验数据是不会LOCK表alter table scott.me_score disable validate constraint score_sid_fk;alter table scott.me_score enable novalidate constraint score_sid_fk;
10.1 、Oracle 约束的DEFERRABLE, NOT DEFERRABLE, INITIALLY IMMEDIATE 和 INITIALLY DEFERRED相关推荐
- Oracle 19c OCP认证 学习笔记(82) —— 约束的DEFERRABLE, NOT DEFERRABLE, INITIALLY IMMEDIATE 和 INITIALLY DEFERRED
一.约束语句 [ CONSTRAINT constraint_name ]{ NOT NULL |NULL |CHECK ( expression ) [ NO INHERIT ] |DEFAULT ...
- oracle银行卡号检查约束,oracle约束学习(1)unique和check
有人说,没有索引, 拿什么来保证约束?姑且不论这话的对错,但约束的实现(除了not null),很多都是通过索引来快速定位约束的地方.unique约束会自动建立索引,pk也是.也因此,约束的很多问题总 ...
- 默认约束 oracle,ORACLE约束(constraint):对象的强制规定
ORACLE约束(constraint):对象的强制规定 5种约束: NOT NULL 非空 NN UNIQUE 唯一 UK PRIMARY KEY 主键 PK FOREIGN ...
- oracle约束什么意思,Oracle约束的属性
今天处理了一个由于约束插入数据失败的问题,处理时感到有些吃力,三天不练手生 今天处理了一个由于约束插入数据失败的问题,,处理时感到有些吃力,三天不练手生啊.在这里回忆一下. Oracle数据库Cons ...
- ORACLE约束总结
你对ORACLE约束的了解如何?比较模糊还是相当透彻?如果你对下面几个问题了如指掌的话,恭喜你,你已经对约束掌握得比较好了,不用看这篇文章了.ORACLE的约束有啥功能作用? 有哪些类型约束(不同版本 ...
- oracle10个,OracleDBA新手经常碰到的10个Oracle错误
OracleDBA新手经常碰到的10个Oracle错误(英文) This document contains information about errors frequently encounter ...
- oracle约束 关闭,Oracle约束管理脚本
正在看的ORACLE教程是:Oracle约束管理脚本. 作为一个Oracle数据库管理员,会碰到这样的数据库管理需求,停止或者打开当前用户(模式)下所有表的约束条件和触发器.这在数据库的合并以及对数据 ...
- (学习笔记)Oracle约束
约束指表中的字段要满足某些要求或条件,又或者带有某些意义 1.主键约束:作用是保证一条数据的唯一性.例如,有一张班级姓名表,某一天来了两位同学,恰好两位同学是同名同姓,则需要一个非空且唯一的字段来区别 ...
- 2017年10月 oracle 关键补丁更新
注:本文出自http://www.oracle.com/technetwork/security-advisory/cpuoct2017-3236626.html 检查你的系统,是否打了最新的补丁.. ...
最新文章
- 关于ML.NET v0.7的发布说明
- 【在路上4】在派件时效分析中剥离有效因素
- mysql数据库rp集群,使用MySQL-Cluster搭建MySQL数据库集群
- c语言通过指针变量输出10个元素,C语言程序设计第2版指针程序设计(10页)-原创力文档...
- XSS攻击及解决方案
- FFmpeg之wav转mp3(二十四)
- 超好用的数学教学软件:几何画板Sketchpad for Mac中文版
- 时间刻度线css,纯CSS时间轴列表
- docker 构建推送到阿里云仓库失败
- word 加载MathType打开时显示“安全警告,宏已被禁用”解决办法
- 你炒的肉丝为何又柴又老又难吃?
- APP自动化测试之录制脚本:3.运行录制的脚本
- js清空input类型为type的文件框的内容
- linux 内存管理之kmalloc、vmalloc、malloc、get_gree_pages的区别
- 超详细!apk安装包快速反编译,多种反编译及失败的解决方案(包含classes.dex的反编译,新增加快速反编译)
- android手机Down版本
- python镜像安装第三方库
- 防雷器的综合应用案例
- 如何查去别人的ip,进行定位
- html怎么制作公告滚动字幕,实现公告文字轮播效果