当前位置:网站首页>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;
边栏推荐
- Network protocol learning -- lldp protocol learning
- Get to know Prometheus
- Shardingsphere proxy 5.0 sub database and sub table (I)
- Sign up to open the third session of the "flying oar hacker marathon". It's been a long time
- MCU development -- face recognition application based on esp32-cam
- [the path of system analyst] Chapter 6: Double inventory demand engineering (comprehensive knowledge concept)
- Shen Lu, China Communications Institute: police open source Protocol - ofl v1.1 Introduction and Compliance Analysis
- 金仓KFS数据集中场景(多对一)部署
- Handling of NPM I installation problems
- QT: parsing JSON
猜你喜欢

WPF prism framework

Detailed explanation of Android interview notes handler

Netease's open source distributed storage system curve officially became the CNCF sandbox project
![[paper reading | deep reading] drne:deep recursive network embedding with regular equivalence](/img/48/4e8d367b49f04a2a71a2c97019501f.png)
[paper reading | deep reading] drne:deep recursive network embedding with regular equivalence

Garbage collection mechanism

The real difference between i++ and ++i

Opencv learning (II) -- installing opencv on raspberry pie

OpenCV学习(一)---环境搭建
![[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?

Complete steps for a complete Oracle uninstall
随机推荐
金仓数据库 KingbaseES 插件identity_pwdexp
CSRF攻击
Open source invites you to participate in the openssf Open Source Security Online Seminar
[file containing vulnerability-03] six ways to exploit file containing vulnerabilities
Five types of questions about network planning
Previous string inversion topic
16 enterprise architecture strategies
OpenCV学习(二)---树莓派上安装opencv
金仓数据库 KingbaseES 插件ftutilx
Get to know Prometheus
西门子PLCS7-200使用(一)---开发环境和组态软件入门
【图像融合】基于形态学分析结合稀疏表征实现图像融合附matlab代码
A random number generator
性能之文件系统篇
之前字符串反转的题目
Flask blog practice - realize the latest articles and search in the sidebar
[paper reading | depth] role based network embedding via structural features reconstruction with degree regulated
一个五年北漂的技术er,根据这些年的真实经历,给应届生的一些建议
FPGA基于VGA显示字符及图片
Flask blog practice - realize personal center and authority management