当前位置:网站首页>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;
边栏推荐
- Netease's open source distributed storage system curve officially became the CNCF sandbox project
- Server rendering
- 单片机进阶---PCB开发之照葫芦画瓢(二)
- OpenCV学习(一)---环境搭建
- 今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
- 之前字符串反转的题目
- 我的作文题目是——《我的区长父亲》
- The title of my composition is - "my district head father"
- 金仓数据库 KingbaseES 插件dbms_session
- Continuous delivery jenkinsfile syntax
猜你喜欢

OpenCV学习(一)---环境搭建
![[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology](/img/82/8cac87231e51698ab17f1274b3a0bd.jpg)
[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology
![[200 opencv routines] 210 Are there so many holes in drawing a straight line?](/img/1e/5b8245eb1c391649c7b2783c62c2b0.png)
[200 opencv routines] 210 Are there so many holes in drawing a straight line?

单片机开发---基于ESP32-CAM的人脸识别应用

中國信通院沈瀅:字體開源協議——OFL V1.1介紹及合規要點分析

TASK03|概率论

A five-year technical Er, based on the real experience of these years, gives some suggestions to the fresh students

Es learning

FPGA基于VGA显示字符及图片

视频会议一体机的技术实践和发展趋势
随机推荐
Is it safe to open an account through mobile phone if you open an account through stock speculation? Who knows?
June 24, 2022: golang multiple choice question, what does the following golang code output? A:1; B:3; C:4; D: Compilation failed. package main import ( “fmt“ ) func mai
Complete steps for a complete Oracle uninstall
16 enterprise architecture strategies
Explanation and use of kotlin syntax for Android
《天天数学》连载52:二月二十日
网络远程访问的方式使用树莓派
软件测试 避免“试用期被辞退“指南,看这一篇就够了
Performance file system
Your driver settings have been set to force 4x antialiasing in OpenGL applications
有关计网的五种类型题
Advanced single chip microcomputer -- development of PCB (2)
Unreal Engine graphics and text notes: use VAT (vertex animation texture) to make Houdini end on Houdini special effect (ue4/ue5)
金仓数据库 KingbaseES 插件force_view
[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology
[paper reading | deep reading] drne:deep recursive network embedding with regular equivalence
Server rendering
Is it safe to open an account with Guangzhou securities by mobile phone?
FPGA基于VGA显示字符及图片
成长:如何深度思考与学习