Question

I want to have a one-to-many relationship in which for each parent, one or zero of the children is marked as a “favorite.” However, not every parent will have a child. (Think of the parents as questions on this site, children as answers, and favorite as the accepted answer.) For example,

TableA
    Id            INT PRIMARY KEY

TableB
    Id            INT PRIMARY KEY
    Parent        INT NOT NULL FOREIGN KEY REFERENCES TableA.Id

The way I see it, I can either add the following column to TableA:

    FavoriteChild INT NULL FOREIGN KEY REFERENCES TableB.Id

or the following column to TableB:

    IsFavorite    BIT NOT NULL

The problem with the first approach is that it introduces a nullable foreign key, which, I understand, is not in normalized form. The problem with the second approach is that more work needs to be done to ensure that at most one child is the favorite.

What sort of criteria should I use to determine which approach to use? Or, are there other approaches I am not considering?

I am using SQL Server 2012.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top