What is the best database schema to support values that are only appropriate to specific rows?

前端 未结 5 1443
南旧
南旧 2020-11-29 07:43

I have a db table called Calendar with fields

  1. Id (PK)
  2. Name
  3. Description
  4. CalendarTypeId (FK into CalendarType
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 08:24

    Maybe I'm looking at this too simply, but if you stick to the model of "use before reuse" then the right thing to do is simply add the null-able column to your calendar table and add a check constraint back to calendar type ensuring it's not null if calendar type = 2.

    It's straight forward, and most of all it's easily testable.

    I might get some slack for this answer (not the most efficient probably), but it completely depends on the scale of your solution. The reality is these constraints could very well change in the next couple months and you don't want to paint yourself into a corner by picking the "right" way when you don't know what that is yet. It's entirely possible that when you get to the 10th calendar type a pattern will emerge which really will tell you the best (or most normal) way to do it. For now, just keep it simple and make it easy to test and easy to change later.

提交回复
热议问题