ORACLE约束(constraint):对象的强制规定

5种约束:

NOT NULL     非空 NN

UNIQUE       唯一 UK

PRIMARY KEY  主键 PK

FOREIGN KEY  外键 FK

CHECK        条件 CK

约束创建:

1)创建对象时定义

2) 创建对象后定义

3) 约束有名字:默认:SYS_C0, 系统推荐:表名_列名_约束类型

create table scott.tab

drop 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/DISABLED

VALIDATED/NOVALIDATED

DEFERRABLE/NON-DEFERRABLE

DEFERRED/IMMEDIATE

RELY/NORELY

1)DEFERRABLE/NON-DEFERRABLE,DEFERRED/IMMEDIATE

deferrable:     constraint如果被定义成deferrable那么constraints可以在deferred和imediate两种状态相互转换

not deferrable: constraint默认是not deferrable,同initially immediate,不能在deferred和imediate两种状态相互转换

deferred:       意味着constraint将被延迟即在transaction过程中使constraint失效,等到如果transaction commit时transaction会变成immediate

immediate:      意味着constraint在transaction过程中使constraint一直有效

deferrable initially immediate: 允许将constraint再改为initially deferred

deferrable initially deferred:  允许将constraint再改为initially immediate

drop 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=>deferred

set constraint scott.check_x deferred;

insert into scott.test values (-1,1 );

1 row created.

commit;

ERROR at line 1:

ORA-02091: transaction rolled back

ORA-02290: check constraint (SCOTT.CHECK_X) violated

--initially deferred:constraint被延迟,transaction commit时transaction会变成immediate

insert into scott.test values ( 1,-1 );

1 row created.

commit;

ERROR at line 1:

ORA-02091: transaction rolled back

ORA-02290: check constraint (SCOTT.CHECK_Y) violated

--转换initially deferred=>immediate

set constraint scott.check_y immediate;

insert into scott.test values ( 1,-1 );

ERROR at line 1:

ORA-02290: check constraint (SCOTT.CHECK_Y) violated

1)enable/disable validate/novalidate

enable/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;

默认约束 oracle,ORACLE约束(constraint):对象的强制规定相关推荐

  1. Oracle数据库:约束条件:主键约束、唯一约束、检查约束、非空约束、外键约束、默认值填写

    Oracle数据库:约束条件:主键约束.唯一约束.检查约束.非空约束.外键约束.默认值填写 2022找工作是学历.能力和运气的超强结合体,遇到寒冬,大厂不招人,可能很多算法学生都得去找开发,测开 测开 ...

  2. oracle之约束(constraint)

    1 什么是约束(constraint) 数据库约束是对表中的数据进行进一步的限制,保证数据的正确性.有效性和完整性. 2 注意事项 如果不指定约束名 ,Oracle server 自动按照 SYS_C ...

  3. oracle删除unique key,概述Oracle Unique约束

    Oracle还是比较常用的,于是我研究了一下Oracle Unique约束,在这里拿出来和大家分享一下,希望对大家有用.如果某个约束只作用于单独的字段,即可以在字段级定义约束,也可以在表级定义约束,但 ...

  4. Oracle的约束和索引

    [导读]如果某个约束只作用于单独的字段,即可以在字段级定义约束,也可以在表级定义约束,但如果某个约束作用于多个字段 Oracle的约束 * 如果某个约束只作用于单独的字段,即可以在字段级定义约束,也可 ...

  5. oracle建表唯一约束语句,Oracle建表语句是什么

    Oracle建表语句是什么 oracle数据库的建表语句,具体语法如下:CREATE TABLE tablename( column_name datatype [null,not null], co ...

  6. Oracle外键约束reference,oracle外键约束

    -- 创建测试主表. ID 是主键. CREATE TABLE test_main ( id      INT, value   VARCHAR(10), PRIMARY KEY(id) ); --  ...

  7. Oracle视图添加约束,Oracle的约束视图

    在Oracle中插入数据时,可能由于不符合约束而无法插入成功. 将报类似于如下的错误信息: SQL Error: ORA-02291: 违反完整约束条件 (TEST.FK_KB_TEST_ID) - ...

  8. oracle 怎么创建约束,Oracle创建约束

    约束的类型: oracle数据库支持的约束类型包括: 1.unique 2.not null 3.primary key 4.foreignkey 5.check 约束都有名称.如果没有显示地给它们命 ...

  9. oracle 主键约束复制,Oracle主键及约束

    Oracle主键Primary Key包含非空约束及唯一约束. 添加主键的语句 alter table table_nameadd constraint cons_name primary key(c ...

最新文章

  1. Mozilla停止对Firefox Hello的支持(采访)
  2. python 修改文件名_Python 批量修改文件名
  3. nginx+ssl+pm2 部署 nodejs 服务
  4. stateOffset
  5. ubunut16.04解决网速慢的办法
  6. 云开发是啥?看看它在编程导航项目的实践
  7. NASM汇编语言与计算机系统11-9号与0X16号中断显示键盘输入(int)
  8. 网易云音乐歌单解析下载源码
  9. Java(58):maven test 运行特定单元测试类
  10. Qt + DbgView : 将qDebug输出到DbgView或终端
  11. android应用流程图,Android APP 启动流程简析
  12. 第一章课后习题源代码(笔记自用)
  13. Ubuntu开启SSH服务以及使用Putty远程控制的解决办法
  14. 百度网盘如何提高下载速度
  15. AI 场景存储优化:云知声超算平台基于 JuiceFS 的存储实践
  16. 支付宝生活号h5网页--蚂蚁认证
  17. 微信小程序的一些新手示例(¥62)
  18. 15、JVM监控及诊断工具-GUI篇
  19. wordpress:主题-一个完整的WP主题通常包含以下模板文件
  20. 博科SAN交换机基本配置(华为SNS系列交换机为例OEM博科)

热门文章

  1. 最小生成树-graphics动画设计
  2. Python len()
  3. 【MyEclipse中,将WebContent改为WebRoot的方法】
  4. excel图文教程:九九乘法表的制作方法
  5. matlab 中netff,newff函数里的参数设置方法
  6. js倒计时----距离活动结束还有 xx天xx时xx分xx秒
  7. 音视频常见概念:软解硬解、IBP帧、GOP、YUV编码
  8. 在Linux下用tftp刷写路由器固件
  9. 数据特征分析:对比分析
  10. 如何将光盘里面的内容复制到电脑中