say that i have a one to many relations where there are two tables, a Person table and a Belonging table. Now, each Person has ONLY ONE favorite belonging and a specific belonging cannot belong to another person as well.
My question is, where would that information be better kept ? In the Person table as a favorite_belonging_id or in the Belonging table as an is_favorite entry ? To my eyes, the first choice seems to be the better version, but I would like to hear what sql knowledgeable people have to say about it.
EDIT : A Person has many belongings but only ONE favorite belonging and each belonging can only belong to one person. It’s a one to many association.
I’d be tempted to go with your first suggestion (a
favourite_belonging_idcolumn in thePersontable), as one can then create a foreign key reference from(person_id, favourite_belonging_id)to(owner_id, belonging_id)in theBelongingtable.If one were to go the other route of creating a
is_favouriteflag in theBelongingtable, there is no obvious way of ensuring the 1:1 nature of person-favourite belonging relationships (a compositeUNIQUEindex over(owner_id, is_favourite)would fail when a person has multiple belongings that are not their favourite).That said, it doesn’t feel like this information really belongs in the
Persontable, as it isn’t really a property of the person but rather it’s a property of theBelonging. If you feel strongly about it, you could create aFavouritestable that has aUNIQUE(orPRIMARY) index overperson_id.