Four pairs of attributes of the Constraint in Oracle Database

Source: Internet
Author: User

We will create someConstraintConstraints, including primary keys and Foreign keys. Does it have attributes? The answer is yes. In this article, we will introduceOracleFour attributes of database Constraint constraints: Deferrable/not deferrable, Deferred/immediate, enalbe/disable, validate/novalidate. Let's take a look at this process.

1. Deferrable, not deferrable (default value)

1) This defines whether the attribute can be defer. defer refers to the time for checking. If the check is Defer at commit, otherwise it is immediate. you can set another property 2 -- Deferred, immediate.

2) There are two ways to set defer check (the premise is that the contraint of Deferrable is created)

A. Specify the Deferred value when creating a contraint

B. Modify statements at the session level

Set constraint (s) contraint_name/all immediate/deferred.

3) This attribute is defined when the constraint is created and cannot be modified.

4) notice: If a Deferrable uk or pk is created, only the corresponding nonuniquce index is created, and the uniquce index is not created.

2. Deferred, immediate (default value)

1) Whether defer. Deferred: check on commit; immediate: check immediate.

2) If constraint is not deferrable, immediate is only choice.

3) For example:

 
 
  1. Create table games
  2.  
  3. (Scores NUMBER, CONSTRAINT unq_num UNIQUE (scores)
  4.  
  5. Initially deferred deferrable );
  6.  
  7. Insert into games values (1 );
  8.  
  9. Insert into games values (1 );
  10.  
  11. Commit; -- an error is reported here
  12.  
  13. You will not get a error util you commit it;
  14.  
  15. Set constraint (s) unq_num immediate; -- modify attributes
  16.  
  17. Insert into games values (2 );
  18.  
  19. Insert into games values (2); -- an error is reported here.
  20.  
  21. Commit;
  22.  
  23. You will get a error when you execute the second SQL;

3. novalidate, validate (default value)

1) This defines whether the attribute constraint checks existing data in the table, for example:

 
 
  1. Create table t (id number );
  2.  
  3. Insert into t values (1 );
  4.  
  5. Insert into t values (2 );
  6.  
  7. Alter table t add constraint ch_100 check (id> = 100); -- failed
  8.  
  9. Alter table t add constraint ch_100 check (id> = 100) novalidate; -- successful

2) notice: The contraint (such as pk and uk) related to the unique index must be set to Deferrable (only a non-unique index is created) to achieve the above effect ), if the uniqueness is violated, an error is returned. Therefore, a non-uniqueness index must be created. for example:

 
 
  1. Drop table t;
  2.  
  3. Create table t (id number );
  4.  
  5. Insert into t values (1 );
  6.  
  7. Insert into t values (1 );
  8.  
  9. Alter table t add constraint ch_100 unique (id); -- Error
  10.  
  11. Alter table t add constraint ch_100 unique (id) novalidate; -- Error
  12.  
  13. Alter table t add constraint ch_100 unique (id) deferrable novalidate; -- successful

4. disable, enalbe (default value)

1) Enable and disable constraint. disable is defined when pk and uk are created, and no corresponding index is created.

 
 
  1. ALTER TABLE dept DISABLE CONSTRAINT dname_ukey;  
  2.  
  3. ALTER TABLE dept ENABLE CONSTRAINT dname_ukey;  
  4.  
  5. alter table t add constraint ch_100 unique(id) disable; 

2) What does DISABLE uk or pk do:

Disable non-deferrable pk and uk will delete the corresponding index (unless the keep index is specified, but the index after the keep is unique, and the uniqueness check should be performed during data insert ), re-indexing during enable.

The primary key and uk of Disbale deferrable will retain the original index (because the original index is non-unique and does not affect the insert operation ).

3) Some operation experience:

Note the following for keep index:

A. alter table games DISAble CONSTRAINT fk_num keep index; -- the unique index is retained, So duplicate data cannot be inserted.

B. alter table games DISAble CONSTRAINT fk_num; -- if the statement is executed in the previous step, the statement will not do anything, and the unique index will still be retained. In this case, enable in the disable. if the original status is able, the unique index will be deleted.

Here is an introduction to the four attributes of the Oracle database constraint constraints. I hope this introduction will be helpful to you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.