ForeignKey Referencing Same Table

后端 未结 5 1613
栀梦
栀梦 2021-02-05 22:08

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 mo         


        
相关标签:
5条回答
  • 2021-02-05 22:35

    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'
    
    0 讨论(0)
  • 2021-02-05 22:36
    SELECT * 
    FROM Person 
    WHERE father_id = (SELECT id FROM Person WHERE name = 'John Smith')
    AND    mother_id = (SELECT id FROM Person WHERE name = 'Jane')
    
    0 讨论(0)
  • 2021-02-05 22:40

    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

    0 讨论(0)
  • 2021-02-05 22:42

    The answer was already given by podiluska, just explaining how it works since it looks like you're new to MySql.

    By giving an Alias to a table(like mother or father to the table person) you do something like a pseudo-table, that the MySql interprets as another table, so the join happens normally, just imagine there are 3 tables now. Person, Father and Mother, and they are all linked together by the join.

    0 讨论(0)
  • 2021-02-05 22:43

    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..

    0 讨论(0)
提交回复
热议问题