Nullable Foreign Key bad practice?

前端 未结 11 899
猫巷女王i
猫巷女王i 2020-11-28 03:51

Let\'s say you have a table Orders with a foreign key to a Customer Id. Now, suppose you want to add an Order without a Customer Id, (whether that should be possible is anot

11条回答
  •  醉酒成梦
    2020-11-28 04:16

    You could always add an artificial row to your Customer table, something like Id=-1 and CustomerName = 'Unknown' and then in cases when you would normally set your CustomerId in Order NULL set it to -1.

    This allows you to have no nullable FKs but still represent the lack of data appropriately (and will save you from downstream users not knowing how to deal with NULLs).

提交回复
热议问题