Oracle foreign key constraint processing for null

Source: Internet
Author: User

Oracle processes null. Simply put, if multiple columns are unique and contain more than one null, Oracle requires columns not null to be unique.
However, this method is not used for foreign key processing:
SQL> Create Table t_p (ID number, name varchar2 (30), constraint pk_t_p primary K
Ey (ID, name ));
Table created.
SQL> Create Table T_c (ID number, f_id number, f_name varchar2 (30 ),
2 constraint fk_t_c foreign key (f_id, f_name) References t_p );
Table created.
SQL> insert into t_p values (1, 'A ');
1 row created.
SQL> insert into T_c values (1, 1, 'A ');
1 row created.
SQL> insert into T_c values (1, 1, 'B ');
Insert into T_c values (1, 1, 'B ')
*
Error at line 1:
ORA-02291: Integrity Constraint (Scott. fk_t_c) violated-parent key not found
SQL> insert into T_c values (1, null, 'B ');
1 row created.
SQL> insert into T_c values (1, null, 'B ');
1 row created.
SQL> select * From T_c;
Id f_id f_name
--------------------------------------------------
1 1
1 B
1 B
SQL>
As you can see, the f_id column is empty, so that Oracle no longer checks whether other columns meet the foreign key constraints, so that this record is directly inserted into the subtable.
Generally, the combination of Foreign keys is rarely used, so this problem has never been noticed before. This description is only available at the time of reading the document.
This article does not discuss whether such an implementation is justified. At least Oracle thinks that this implementation is justified, and it has already been implemented in this way, and we cannot change it. What we can do is to carefully consider whether to allow the foreign key column to be empty when using the composite foreign key.

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.