Difference between disable/enable validate/novalidate (Oracle)

Source: Internet
Author: User
Enable constraints:
Enable (validate): Enable constraints, create indexes, and execute constraints on existing and new data.
Enable novalidate: Enable constraints, create indexes, and only enforce constraints on new data, regardless of existing data in the table.

Forbidden constraint:
Disable (novalidate): disables the constraint and deletes the index. You can modify the data of the constraint column.
Disable validate: disables constraints and deletes indexes. You cannot insert, update, or delete a table.

Environment: Oracle 9i 9.0.1.0 for win, all of the above conclusions have passed the test.
For example, after the disable validate constraint is executed, the update... Operation prompt is displayed:
ORA-25128: cannot insert/update/delete a table with a disabled and validation constraint (SYS. pk_emp_01)

========================================================== ==================================


Enable and disable constraint: Enable validate; Enable novalidate; Disable validate; Disable novalidate;
1. There is nothing to say. 2 indicates that existing data is not checked when enabling, and 4 indicates that future data is not checked.
Perform the following tests:
1. First create a test table
Create Table A (no number (5), name varchar2 (10 ));
Insert into a values (1, 'ss ');
Alter table a add constraint pK_a primary key (NO );

Create Table B (no number (5), name varchar2 (10 ));
Insert into a values (1, 'ss ');
Alter table B add constraint fk_a_ B
Foreign key (NO)
References A (NO );

2. Test FK
SQL> insert into B values (2, 'sd ');
Insert into B values (2, 'sd ')
*
Error at line 1:
ORA-02291: Integrity Constraint (system. fk_a_ B) violated-parent key not found
Use
SQL> alter table B disable novalidate constraint fk_a_ B;

Table altered.

Insert into B values (2, 'sdd ')
SQL>/

1 row created.
SQL> alter table B enable novalidate constraint fk_a_ B;

Table altered.
From the test results above, we can see that the constraints such as enable novalidate and disable novalidate are useful.
3. Test PK
SQL> ALTER TABLE a disable novalidate primary key;

Table altered.
SQL> INSERT INTO A values (1, 'sd ');

1 row created.

SQL> commit;

Commit complete.

SQL> ALTER TABLE A enable novalidate primary key;
Alter table a enable novalidate primary key
*
Error at line 1:
ORA-02437: cannot validate (system. sys_c001578)-primary key violated
In the PK test, enable novalidate cannot be used.
According to kamus, the Enable novalidate primary key must be unique.
I checked the relevant information and did not find the above description, but found the following statement:
Primary and unique keys must use nonunique Indexes
Test from new
4. Test PK (2)
SQL> ALTER TABLE a disable novalidate primary key;

Table altered.
In this case, I checked table A and found that the index pK_a corresponding to the primary key is missing.
Create an index manually
SQL> Create index I _a on a (NO );

Index created.

SQL> ALTER TABLE A enable novalidate primary key;

Table altered.
Succeeded.

Conclusion:
From the test results, we can see that novalidate works properly when it is not a PK; UN.
When using PK; UN, you must first create the relevant index, and then use novalidate.

Validated Constraints
You have seen how to enable and disable a constraint. Enable and disable
Affect only future data that will be added/modified in the table. In contrast,
The Validate and novalidate keywords in the alter table command act
Upon the existing data. Therefore, a constraint can have four states:

Enable validateThis is the default for the Enable clause. The existing
Data in the table is validated to verify that it conforms to the constraint.

Enable novalidateDoes not validate the existing data, but enables
The constraint for future constraint checking.

Disable validateThe constraint is disabled (any index used
Enforce the constraint is also dropped), but the constraint is kept valid.
No DML operation is allowed on the table because future changes cannot
Be verified.

Disable novalidateThis is the default for the disable clause.
Constraint is disabled, and no checks are done on future or existing data.

Eg:
Alter table wh01 modify constraint pk_wh01
Disable novalidate;

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.