当前位置:网站首页>10.1. Oracle constraint deferred, not deferred, initially deferred and initially deferred
10.1. Oracle constraint deferred, not deferred, initially deferred and initially deferred
2022-06-25 11:05:00 【Anonymous & Xiaoyu】
- Constrained DEFERRABLE, NOT DEFERRABLE, INITIALLY IMMEDIATE and 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 ( Constraint checks that can be deferred ): It can be postponed until the end of the transaction . At present, only UNIQUE, PRIMARY KEY, EXCLUDE and FOREIGN KEY To support DEFERRABLE. NOT NULL and CHECK Constraint not supported .
NOT DEFERRABLE ( Non deferrable constraint checks ): The constraints are verified immediately after each command . The default is NOT DEFERRABLE.
If the constraint is INITIALLY IMMEDIATE, Then check it after each statement . This is the default .
If the constraint is INITIALLY DEFERRED, Then do not check until the transaction is completed .
The check time can be modified with the following command :
SET CONSTRAINTS [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
Example :
The default is 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');
Created 1 That's ok .
SQL> insert into t1 values(1,'Tough1');
insert into t1 values(1,'Tough1')
The first 1 Line error :
ORA-00001: Violate the only constraint (SCOTT.SYS_C005428)
DEFERRABLE or I NITIALLY DEFERRED The situation of :
create table t2 (id number, name char(10));
alter table t2 modify id number primary key INITIALLY DEFERRED;
or
alter table t2 modify id number primary key DEFERRABLE INITIALLY DEFERRED;
SQL> insert into t2 values(1,'Tough1');
Created 1 That's ok .
SQL> insert into t2 values(1,'Tough1');
Created 1 That's ok .
SQL> commit;
commit
The first 1 Line error :
ORA-02091: Transaction rolled back
ORA-00001: Violate the only constraint (SCOTT.SYS_C005435)
5 Species constraints :
NOT NULL Non empty NN
UNIQUE only UK
PRIMARY KEY Primary key PK
FOREIGN KEY Foreign keys FK
CHECK Conditions CK
Constraint creation :
1) Define... When creating objects
2) Define after object creation
3) Constraints have names : Default :SYS_C0, System recommendation : Table name _ Name _ Constraint type
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;
or
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;
-- Foreign keys
-- Refer to integrity constraints
-- Limit (Restrict). Modification or deletion is not allowed . If you modify or delete the primary key of the primary table , If there are child records in the child table , The system will generate an error prompt . This is the default referential integrity setting .
-- empty (Set Null). If the foreign key column is allowed to be empty , If you modify or delete the primary key of the primary table , Set the foreign key column referenced in the sub table to null (NULL).
-- Set as default (Set Default). If a default value is specified , If you modify or delete the primary key of the primary table , Set the foreign key referenced in the sub table to the default value (Default).
-- cascade (Cascade). When changing the primary key in the main table to a new value , Modify the value of the foreign key in the sub table accordingly ; Or delete the record of the primary key in the main table , Delete the foreign key record in the sub table accordingly .
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;
or
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;
-- Query constraints
select * from dba_constraints where table_name like '%me_stu%';
select * from dba_cons_columns where table_name like '%me_stu%';
-- Delete constraints
alter table scott.me_stu drop constraints stu_nam_uk;
alter table scott.me_score drop constraints score_sid_fk;
-- Adding constraints
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;
-- Foreign key pair DML influence
INSERT:
1) The child table has no effect on the parent table
2) When inserting a foreign key in a child table , Parent table must exist
DELETE:
1) Pairs of sub tables DELETE The operation has no effect on the parent table
2) To the parent table DELETE You must delete the sub table first
UPDATE:
1) It affects both parent and child tables
2) If a parent table is referenced by a child table, it cannot be updated
3) Pairs of sub tables UPDATE In operation , The foreign key must exist in the parent table
-- Foreign key pair DDL influence
1) It has no effect on the sub table , If a parent table is referenced by a child table, it cannot be deleted
-- To eliminate the impact, you can add clauses when you create constraints :
on delete set null When the main table is deleted , The sub table is empty
on delete cascade When the main table is deleted , Cascading deletion of child tables
-- Disable and enable constraints
alter table scott.me_score disable constraint score_sid_fk;
alter table scott.me_score enable constraint score_sid_fk;
-- constraint (constraint) state :
ENABLED/DISABLED
VALIDATED/NOVALIDATED
DEFERRABLE/NON-DEFERRABLE
DEFERRED/IMMEDIATE
RELY/NORELY
1) DEFERRABLE/NON-DEFERRABLE,DEFERRED/IMMEDIATE
deferrable: constraint If defined as deferrable that constraints Can be in deferred and imediate The two states switch to each other
not deferrable: constraint The default is not deferrable, Same as initially immediate, Can't be in deferred and imediate The two states switch to each other
deferred: signify constraint Will be delayed at transaction In the process of making constraint invalid , Wait until if transaction commit when transaction Will become immediate
immediate: signify constraint stay transaction In the process of making constraint Always working
deferrable initially immediate: Allows you to constraint Change it to initially deferred
deferrable initially deferred: Allows you to constraint Change it to 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;
or
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: stay transaction In the process of making constraint Always working
insert into scott.test values (-1,1 );
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CHECK_X) violated
-- transformation 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 Delayed ,transaction commit when transaction Will become 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
-- transformation 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: There are constraints on future data / Unrestricted
validate/novalidate: Constraints on existing data / Unrestricted
Enable constraints :
enable( validate) : Enable constraints , Create index , Execute constraints on existing and newly added data .
enable novalidate : Enable constraints , Create index , Enforce constraints only on newly added data , Regardless of the existing data in the table .
Disable constraints :
disable( novalidate): Close constraint , Delete index , You can modify the data of constraint columns .
disable validate : Close constraint , Delete index , The table cannot be modified Insert / to update / Delete and other operations .
Be careful : If you add constraints to a large table , that ORACLE Meeting LOCK This table , then SCAN All the data , To determine whether it meets CONSTRAINT The requirements of , It is obviously not appropriate in a busy system . So use enable、
novalidate More appropriate , because ORACLE Just LOCK Table a short period of time to establish CONSTRAINT, When CONSTRAINT After the establishment VALIDATE, At this time, the inspection data will not LOCK surface
alter table scott.me_score disable validate constraint score_sid_fk;
alter table scott.me_score enable novalidate constraint score_sid_fk;
边栏推荐
- OpenCV学习(二)---树莓派上安装opencv
- XSS attack
- Output reading: apply what you have learned
- 单片机进阶---PCB开发之照葫芦画瓢(二)
- Complete steps for a complete Oracle uninstall
- 服务端渲染
- [200 opencv routines] 210 Are there so many holes in drawing a straight line?
- 撸一个随机数生成器
- Is it safe to open an account with Guangzhou securities by mobile phone?
- [paper reading | deep reading] drne:deep recursive network embedding with regular equivalence
猜你喜欢
Network protocol learning -- lldp protocol learning
scrapy+scrapyd+gerapy 爬虫调度框架
【图像融合】基于形态学分析结合稀疏表征实现图像融合附matlab代码
Dell technology performs the "fast" formula and plays ci/cd
[paper reading | deep reading] drne:deep recursive network embedding with regular equivalence
Oracle彻底卸载的完整步骤
WPF prism framework
OpenCV学习(二)---树莓派上安装opencv
Houdini graphic notes: could not create OpenCL device of type (houdini_ocl_devicetype) problem solving
Shardingsphere proxy 5.0 sub database and sub table (I)
随机推荐
TASK03|概率论
2022年PMP项目管理考试敏捷知识点(2)
Kotlin implements a simple login page
Google Earth Engine(GEE)——evaluate实现一键批量下载研究区内的所有单张影像(上海市部分区域)
Android之Kotlin语法详解与使用
西门子PLCS7-200使用(一)---开发环境和组态软件入门
FPGA基于VGA显示字符及图片
FPGA displays characters and pictures based on VGA
Android:kotlin中Gson与JSON的泛型映射解析
Performance file system
2022-06-24:golang选择题,以下golang代码输出什么?A:1;B:3;C:4;D:编译失败。 package main import ( “fmt“ ) func mai
软件测试 避免“试用期被辞退“指南,看这一篇就够了
金仓数据库 KingbaseES 插件DBMS_OUTPUT
网易开源的分布式存储系统 Curve 正式成为 CNCF 沙箱项目
Dependent properties, dependent additional properties, and type conversions
Coscon'22 lecturer solicitation order
戴尔科技演绎“快”字诀,玩转CI/CD
性能之文件系统篇
ES 学习
A five-year technical Er, based on the real experience of these years, gives some suggestions to the fresh students