Handling violations of unique constraints in Oracle databases

Source: Internet
Author: User

According to the null definition, NULL represents an unknown, so the result of two null comparisons is neither equal nor unequal, and the result is still unknown. Based on this definition, the existence of multiple null values should not violate a unique constraint.

In fact, Oracle is also implemented like this:

Sql> CREATE TABLET (ID Number); The table was created. SQL> ALTER TABLETADD UNIQUE(ID); the table has changed. SQL> INSERT  intoTVALUES(1); created1line. SQL> INSERT  intoTVALUES(1);INSERT  intoTVALUES(1)*The1line error: ORA-00001: Violates the unique constraint (YANGTK. sys_c007300) SQL> INSERT  intoTVALUES(NULL); created1line. SQL> INSERT  intoTVALUES(NULL); created1line. SQL> INSERT  intoTVALUES(NULL); created1Yes.

However, when the unique constraint is a composite field, the situation changes. Based on the description of the Oracle document, the value of a non-empty field cannot be duplicated for a unique constraint on a composite field. That is, if two fields form a unique constraint where one field is empty, the value of the other field cannot be duplicated.

Sql> DROP TABLET PURGE; The table has been deleted. SQL> CREATE TABLET (ID Number, ID2 Number); The table was created. SQL> ALTER TABLETADD UNIQUE(ID, ID2); The table has changed. SQL> INSERT  intoTVALUES(1,1); created1line. SQL> INSERT  intoTVALUES(1,NULL); created1line. SQL> INSERT  intoTVALUES(2,NULL); created1line. SQL> INSERT  intoTVALUES(1,NULL);INSERT  intoTVALUES(1,NULL)*The1line error: ORA-00001: Violates the unique constraint (YANGTK. sys_c007301) SQL> INSERT  intoTVALUES(NULL,NULL); created1line. SQL> INSERT  intoTVALUES(NULL,NULL); created1line. SQL> INSERT  intoTVALUES(NULL,NULL); created1Yes.
View Code

For all null cases, the same as the single field constraint does not cause duplication, but for partially null cases, as shown in the example above, Oracle considers the constraint to be duplicated as long as the non-null part is duplicated.

And this seems to be in conflict with the definition of NULL, the first time I looked at concept did not understand why Oracle is so realized, but this time to look at concept, has been trying to understand.

Because Oracle's unique constraint is dependent on the index implementation, and Oracle's Btree index does not store null values, records with all null keys are not recorded in the index, so there is no violation of the unique constraint, and for partially null records, the index is to record the value, So once the non-null part of the key value has clashed, Oracle considers the unique constraint violated.

Oracle has chosen its own convenient approach here, without fully implementing a unique constraint based on the null definition.

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.