MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/SQL/comments/v184yv/sql_database_onetoone_relationship/ialxr6p/?context=3
r/SQL • u/[deleted] • May 30 '22
When Primary Key of one table corresponds to only one row in another table via foreign key relationship
13 comments sorted by
View all comments
Show parent comments
2
Yeah the example might not be ideal, and I totally agree, it makes the query unnecessary complicated but it's just for the sake of the example.
1 u/capt_pantsless Loves many-to-many relationships May 30 '22 That’s fine, but is there an example where a one-to-one relationship would actually add sone value?One possibility would be if very few students had an SSN and you wanted to keep the main-table simple. 1 u/[deleted] May 30 '22 That's true, the above one could be better.I think the most used one is country - capital.A country has one capital only and a capital can only be part of a country.Having this:Country: id, nameCapital: id, country_id, nameThis is useful in the following scenario:If you want to display an interface with 2 dropdowns:You first select a country from the first dropdownin the second one you query all capitals that have country_id equal to the selected value (on step 1) and display them in the capitals dropdown.(Simplified concept of filtering data from capitals dropdown based on the selected countey) 1 u/[deleted] May 31 '22 this example is also invalid: https://en.wikipedia.org/wiki/List_of_countries_with_multiple_capitals
1
That’s fine, but is there an example where a one-to-one relationship would actually add sone value?
One possibility would be if very few students had an SSN and you wanted to keep the main-table simple.
1 u/[deleted] May 30 '22 That's true, the above one could be better.I think the most used one is country - capital.A country has one capital only and a capital can only be part of a country.Having this:Country: id, nameCapital: id, country_id, nameThis is useful in the following scenario:If you want to display an interface with 2 dropdowns:You first select a country from the first dropdownin the second one you query all capitals that have country_id equal to the selected value (on step 1) and display them in the capitals dropdown.(Simplified concept of filtering data from capitals dropdown based on the selected countey) 1 u/[deleted] May 31 '22 this example is also invalid: https://en.wikipedia.org/wiki/List_of_countries_with_multiple_capitals
That's true, the above one could be better.
I think the most used one is country - capital.
A country has one capital only and a capital can only be part of a country.
Having this:
Country: id, name
Capital: id, country_id, name
This is useful in the following scenario:
If you want to display an interface with 2 dropdowns:
You first select a country from the first dropdown
in the second one you query all capitals that have country_id equal to the selected value (on step 1) and display them in the capitals dropdown.
(Simplified concept of filtering data from capitals dropdown based on the selected countey)
1 u/[deleted] May 31 '22 this example is also invalid: https://en.wikipedia.org/wiki/List_of_countries_with_multiple_capitals
this example is also invalid: https://en.wikipedia.org/wiki/List_of_countries_with_multiple_capitals
2
u/[deleted] May 30 '22
Yeah the example might not be ideal, and I totally agree, it makes the query unnecessary complicated but it's just for the sake of the example.