• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

SQL Server Nullable Foreign Key Normalization Question

Hello all,

I have an application that has a table currently Customer that has a non nullable field LocationId.  It is a 1 to 1 relationship currently.   We have determined that we have to decouple the LocationId as the Customer may not have a location.   Right now the LocationId is a FK in the Customer table to a PK table called Location.

The question is this should we just make the LocationId a nullable field in the Customer table.   This causes least impact on changes we need to make as we use Entity Framework in our app and it's everywhere.    The other option is making a table called CustomerLocation that has the two PK fields CustomerId and LocationId and deleting it from the Customer table.   This really looks to be a many to many although I can put a constraint on it.   This though we have to in our app go after the FirstOrDefault record everywhere.  If it is best to do it this way then we will just have to bite the bullet.

Any suggestions what is the best practice here?   Having a nullable FK not sure is best but impact is higher on the app code.

Thanks all
0
sbornstein2
Asked:
sbornstein2
3 Solutions
 
Brian CroweCommented:
There is nothing wrong with having a nullable foreign key field and that is the route I would suggest.
0
 
Scott PletcherSenior DBACommented:
If it is (still) a 1-1, and you expect that it will remain a 1-1 even though you've decoupled them, then I would use a nullable foreign key.  There's absolutely no design issue with that, if the data is truly a zero or one occurrence, never more than one.
0
 
Anthony PerkinsCommented:
If on the other hand two or more customers can have the same location then your second option would make more sense.
0
 
sbornstein2Author Commented:
awesome thanks all.   Appreciate the feedback its going to be a 0 to 1 or 1 to 1.
0
 
sbornstein2Author Commented:
thanks all.  giving the top points to Scott good explanation I can use to back myself up as well 0 to 1 or 1 to 1.  Thanks all for the feedback.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now