当前位置:网站首页>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

  1. 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 ]
  1. 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;

原网站

版权声明
本文为[Anonymous & Xiaoyu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200540219466.html