Is it not possible that foreign key(single column) in a child table references to a parent key which has some duplicate values?
3 Answers
By the SQL standard, a foreign key must reference either the primary key or a unique key of the parent table. If the primary key has multiple columns, the foreign key must have the same number and order of columns. Therefore the foreign key references a unique row in the parent table; there can be no duplicates.
Re your comment:
If T.A
is a primary key, then no you can't have any duplicates. Any primary key must be unique and non-null. Therefore if the child table has a foreign key referencing the parent's primary key, it must match a non-null, unique value, and therefore references exactly one row in the parent table. In this case you can't make a child row that references multiple parent rows.
You can create a child row whose foreign key column is NULL, in which case it references no row in the parent table.

- 538,548
- 86
- 673
- 828
-
Thanks, but if we talk about single-column foreign key such as column 'a' is foriegn key in child table t which references to column 'A' in parent table T, so now is it not possible in any situation that column A may have any duplicate values in table T? does it mean foreign key mist alays refer to a primary key in another table?? – ratsy Jan 02 '12 at 22:58
-
2@ratsy: A foreign key must always refer to a column or columns declared as either PRIMARY KEY or UNIQUE. (Unless you're using MySQL. But you should still target only PRIMARY KEY or UNIQUE columns even in MySQL. Search http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html for "the system does not enforce a requirement that the referenced columns be UNIQUE".) – Mike Sherrill 'Cat Recall' Jan 02 '12 at 23:37
-
@Catcall: True, but @ratsy tagged this question `oracle` so I didn't bring up the non-standard behavior of InnoDB. Even when using InnoDB, I recommend strongly against referencing non-unique parent rows, because it's easy to get confused that way. – Bill Karwin Jan 02 '12 at 23:46
-
Thanks a ton Bill, Catcall for clarifying....also i asked this since in my organization we are collating sum databases & in one i found foreign key referencing to a parent key which has two three duplicate records in its table(parent table), parent key is not defined as primary key here :(....so yes Catcall understood my question, here database which am talking abt is an oracle database still this.. Thanks again Bill, so how should this be corrected.. – ratsy Jan 03 '12 at 21:35
No, it is not possible.
When you define a foreign key constraint on a table, it means there is only one corresponding key on the foreign table. If multiples existed on the foreign table which one would be meant?
Wikipedia has this definition on the Foreign key entry:
A foreign key is a field in a relational table that matches a candidate key of another table
Candidate keys are unique within a table.

- 489,969
- 99
- 883
- 1,009
Yes, it is possible for a foreign key to reference a column with duplicate values.
This can happen if the primary key uses a non-unique index and is not validated when it is created. (But I have never seen a situation like this in real life. As @Bill Karwin pointed out, it would be very confusing. So this may not be a situation you really need to worry about.)
--Create a table with two duplicate rows
create table test1(a number);
insert into test1 values(1);
insert into test1 values(1);
commit;
--Create a non-unique index
create index test1_index on test1(a);
--Use the non-unique index for the primary key, do not validate
alter table test1 add constraint test1_pk primary key (a)
using index test1_index novalidate;
--Build another table with a foreign key to TABLE1
create table test2(a number,
constraint test2_fk foreign key (a) references test1(a));
--Inserting a value that refers to the duplicate value still works.
insert into test2 values(1);
commit;
--The foreign key still works:
--ORA-02291: integrity constraint (TEST2_FK) violated - parent key not found
insert into test2 values(2);
--The primary key works as expected, but only for new values:
--ORA-00001: unique constraint (TEST1_PK) violated
insert into test1 values(1);

- 34,999
- 6
- 74
- 132
-
Wacky. I think this falls under the category of, "why would you do that?" :-) But thanks for the clear example! – Bill Karwin Jan 03 '12 at 00:12
-
Thanks Jonearles for above example!....same situation in database which am using, but why & how ..? – ratsy Jan 03 '12 at 21:40
-
There are several good reasons to use a non-unique index for a primary key; deferrable constraints, more options for rebuilding (specifically parallelism), index existed before the constraint, etc. Look here for information on unique vs. non-unique indexes: http://richardfoote.wordpress.com/2008/06/04/primary-keys-and-non-unique-indexes-whats-really-happening/ The NOVALIDATE is kind of odd. As far as I know, there are no advantages to using that, unless you just need to have some "bad" data. Those cases really should be documented. If it's not documented, maybe it was just a mistake? – Jon Heller Jan 04 '12 at 06:49
-
oh okay..thanks a lot Jonearles for clarifying that for me...db which am talking about has no documentation about those duplicate records though, erroneous db then..okay will get back here if would require any help regarding this..however thanks for explaining well.... – ratsy Jan 04 '12 at 23:16