如何创建多个一对一
我有一个包含许多表的数据库,除了一点之外,它看起来都很好...
Inventory Table <*-----1> Storage Table <1-----1> Van Table
^
1
|-------1> Warehouse Table
使用存储表,因为货车和仓库表相似,但如何在存储和仓库/货车表之间创建关系?它们需要 1 对 1 才有意义,因为存储对象只能有 1 个存储位置和类型。 我确实将 Van/Warehouse 表链接到 StorageId 主键,然后添加一个约束以确保 Van 和 Warehouse 表没有相同的 StorageId,但这似乎可以用更好的方法来完成。
我可以看到几种方法,但它们似乎都是错误的,所以任何帮助都会很好!
I have a database set up with many tables and it all looks good apart from one bit...
Inventory Table <*-----1> Storage Table <1-----1> Van Table
^
1
|-------1> Warehouse Table
The Storage table is used since the Van and Warehouse table are similar but how do I create a relationship between Storage and Warehouse/Van tables? It would make sense they need to be 1 to 1 as a Storage object can only be 1 Storage place and type.
I did have the Van/Warehouse table link to the StorageId primary key and then add a constraint to make sure the Van and Warehouse tables dont have the same StorageId, but this seems like it could be done a better way.
I can see several ways of doing this but they all seem wrong, so any help would be good!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用继承(在实体关系建模中也称为“子类”或“类别”)。一般来说,有3种方式在数据库中表示它:
我通常更喜欢第三种方法,但在应用程序级别强制执行子项的存在和排他性。在数据库级别强制执行这两种方法有点麻烦,但如果 DBMS 支持延迟约束,则可以完成。例如:
这将强制执行排他性(由于
CHECK
)以及孩子的存在(由于CHECK
和FK1
/FK2
的组合)。不幸的是,MS SQL Server不支持延迟约束,但您也许可以将整个操作“隐藏”在存储过程后面并禁止客户端直接修改表。
只需强制执行排他性,无需延迟约束:
STORAGE_TYPE
是一种类型鉴别器,通常是一个整数以节省空间(在上面的示例中,0 和 1 对于您的应用程序来说是“已知的”并进行相应的解释)。VAN.STORAGE_TYPE
和WAREHOUSE.STORAGE_TYPE
可以计算(又名“已计算”)列,以节省存储空间并避免需要CHECK
s。--- 编辑 ---
计算列将在 SQL Server 下工作,如下所示:
不幸的是,SQL Server 需要在外键中使用的计算列才能持久化。其他数据库可能没有这个限制(例如Oracle的虚拟列),这样可以节省一些存储空间。
You are using the inheritance (also known in entity-relationship modeling as "subclass" or "category"). In general, there are 3 ways to represent it in the database:
I usually prefer the 3rd approach, but enforce both the presence and the exclusivity of a child at the application level. Enforcing both at the database level is a bit cumbersome, but can be done if the DBMS supports deferred constraints. For example:
This will enforce both the exclusivity (due to the
CHECK
) and the presence (due to the combination ofCHECK
andFK1
/FK2
) of the child.Unfortunately, MS SQL Server does not support deferred constraints, but you may be able to "hide" the whole operation behind stored procedures and forbid clients from modifying the tables directly.
Just the exclusivity can be enforced without deferred constraints:
The
STORAGE_TYPE
is a type discriminator, usually an integer to save space (in the example above, 0 and 1 are "known" to your application and interpreted accordingly).The
VAN.STORAGE_TYPE
andWAREHOUSE.STORAGE_TYPE
can be computed (aka. "calculated") columns to save storage and avoid the need for theCHECK
s.--- EDIT ---
Computed columns would work under SQL Server like this:
Unfortunately, SQL Server requires for a computed column which is used in a foreign key to be PERSISTED. Other databases may not have this limitation (e.g. Oracle's virtual columns), which can save some storage space.
正如你所说,解决方案有很多种。我建议从最简单的解决方案开始,然后在性能或存储出现问题时进行优化。最简单的解决方案(但在存储方面不是最佳解决方案)是拥有一个存储表,其中包含存储类型列(指示该行代表货车还是仓库),以及货车属性和仓库属性列。在代表货车的行中,仓库属性的列全部为空。在代表仓库的行中,货车属性的列全部为空。
这样,您就可以减少表的数量,并使查询保持良好和简单。如果存储变得紧张,请准备好重新考虑您的决定。
As you say, there are many solutions. I would recommend starting with the simplest solution, then optimising later if performance or storage become problems. The simplest solution (but not optimal in terms of storage) would be to have a Storage table that has a column for storage type (indicating whether the row represents a van or a warehouse), plus columns for Van attributes as well as Warehouse attributes. In a row that represents a Van, the columns for the Warehouse attributes will all be null. In a row that represents a Warehouse, the columns for the Van attributes will all be null.
That way, you cut down on the number of tables, and keep your queries nice and simple. Be prepared to revisit your decision if storage becomes tight.
在我看来,库存物品可能会改变位置,所以我会选择这样的东西。
Somehow seems to me that inventory-items may change locations, so I would go with something like this.