应该在数据库中将其表示为1个表还是3个表?我和我的朋友对此有不同的看法,所以我希望了解对此的一般看法。 (也许应该对这两种解决方案都投一票?)
Create Table Order
// Basic fields of the table
- ID (Primary key)
- CustomerID (integer, with a FK)
- Quantity
- ProductID (integer, with a FK)
// Then depending on user selection, either these fields need to be specified
// (could be factored out to a separate table):
{
- InternalAccountID (integer, with a FK)
- InternalCompanyID (integer, with a FK)
}
// Or these (could be factored out to a separate table):
{
- ExternalAccountNumber (free text string)
- ExternalCompanyName (free text string)
- ExtraInformation (free text string)
}
1表方式:
优点:
性能(一次插入而不是两次插入,FK检查,无联接)
可能会占用较少的空间(额外的表具有开销+索引+额外的ID字段)
一张桌子而不是三个
仅仅为2 + 3个字段拆分到新表几乎是没有道理的(或什么?)
缺点:
可空字段
可能多余的“类型”列(可以跳过)
打破3NF(?)
赞成和反对,以及个人意见。 :)
编辑:我尝试通过使用与我实际使用的实体不同的实体来简化示例,因此任何有关更改模型的建议均无济于事。即请更加关注技术方面,而不是领域模型。
最佳答案
我的看法是,如果
// Then depending on user selection, either these fields need to be specified
// (could be factored out to a separate table):
{
- InternalAccountID (integer, with a FK)
- InternalCompanyID (integer, with a FK)
}
// Or these (could be factored out to a separate table):
{
- ExternalAccountNumber (free text string)
- ExternalCompanyName (free text string)
- ExtraInformation (free text string)
}
始终与订单保持1:1的比例(即您不能有3个accountID),然后将其保留为一张表格。为解决null问题,您可以再添加一列称为InternalCustomer(boolean)或CustomerType(varChar)的列,该列可用于定义内部或外部客户,以了解应在两组字段中选择哪一组具体的客户。
由于我们不知道整个数据库是否会充分利用此数据或架构,因此对此的任何回应都无法完全确定。