Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ForeignKey Referencing Same Table

Tags:

sql

mysql

There was an interview test in which below was the table and structure

 Table Person = id, name, dob, dod, mother_id, father_id
 Primary Key (id)
 Foreign Key mother_id references Person
 Foreign Key father_id references Person

and it was asked

  1. "select all who are mothers"
  2. "select those child who are children of 'John Smith' and 'Jane'

and I was puzzled because I was assuming that foreign key would be linked with some other table as usual. But at that point I failed. do some one know the actual answer and reason?

like image 334
IConfused Avatar asked Oct 03 '12 12:10

IConfused


People also ask

Can foreign key reference the same table?

FOREIGN KEY constraints can reference another column in the same table, and is referred to as a self-reference. A FOREIGN KEY constraint specified at the column level can list only one reference column. This column must have the same data type as the column on which the constraint is defined.

Can you have multiple foreign keys from the same table?

A table may have multiple foreign keys, and each foreign key can have a different parent table. Each foreign key is enforced independently by the database system. Therefore, cascading relationships between tables can be established using foreign keys.

Can two foreign keys in the same table reference the same primary key?

Yes, it is okay to have two fk to the same pk in one table.

Does foreign key allow repetition?

1 Answer. Show activity on this post. can you have a column which is referenced as a foreign key by another table contain duplicate values? No - a foreign key constraint must reference a unique key.


3 Answers

This kind of data structure is called a "Self Referencing Table"

SELECT DISTINCT mothers.*
FROM person
    inner join person mothers on person.mother_id = mothers.id

and

SELECT person.*
FROM person
    inner join person fathers on person.father_id = fathers.id
    inner join person mothers on person.mother_id = mothers.id
WHERE 
    fathers.name='john smith'
and 
    mothers.name='jane'
like image 150
podiluska Avatar answered Nov 15 '22 18:11

podiluska


You can always have foreign key that link to the same table.. There is not problem in that..

See, suppose you are storing a person record in the table, now that person would be having mother and father. And mother and father both are themselves a person.. so, they are itself a record of the same table..

Suppose you have following two records in Person table: -

id     name           age         mother_id
1      xyz            24          5
5      abc            57          6

So, from the above table you see that, person with id = 5 is actually mother of person with id = 1.. So, it is a foreign key referencing the same table..

So, here rather than performing a join operation with a different table, you have to perform join with the same table..

SELECT p2.id FROM
Person p1 join Person p2
WHERE p1.mother_id = p2.id

This query will select the record of mother of your current record..

like image 39
Rohit Jain Avatar answered Nov 15 '22 17:11

Rohit Jain


I was assuming that foreign key would be linked with some other table as usual.

It still is - it references other records in the same Person table.

-- All Mothers
SELECT mom.name
   FROM Person mom
   WHERE EXISTS (SELECT 1 FROM Person WHERE mother_id = mom.id);

-- Children of John Smith and Jane
SELECT kid.name
   FROM Person kid
   INNER JOIN Person mom on kid.mother_id = mom.id
   INNER JOIN Person dad on kid.father_id = dad.id
   WHERE mom.name = 'Jane' AND
   dad.name = 'John Smith';

Have a look at this SQL fiddle here

like image 42
StuartLC Avatar answered Nov 15 '22 17:11

StuartLC