数据库设计问题(FK到不同的表)
考虑一个购物车,允许客户使用可变支付系统。
每个系统都有一组不同的参数:系统A有参考id、xml响应、交易id等属性,系统B有交易id和状态,系统C(支票)只有付款日期。每个系统的状态也不同。
在支付系统选择页面上有支付系统的名称和描述,它们也将存储在数据库中(例如,不硬编码为HTML)。
您将如何设计数据库?
我能想到的最好的事情是为每个系统都有一个表+一个链接到相关表名称的支付系统描述表+订单表中的2个额外字段:支付系统ID(将链接到“描述表”) 、支付记录id(将链接到相关支付系统表中的id)。这还允许提供在支付系统模型之间分离的特定功能(订单通知处理等),而不是使用 if
。可以吗?
如果有的话,该项目是基于 PHP 5、Doctrine 和 MySQL 的。
Consider a shopping cart, allowing customers to use variable payment systems.
Each system has a different set of parameters: system A has such attributes as reference id, xml response, transaction id, system B has transaction id and a status, system C (Cheque) has only payment date. Statuses differ in every system as well.
On the payment system selection page there are name and description of the payment system, which are to be stored in the database too (e.g. not hardcoded to HTML).
How would you design the database?
The best thing I can think about is having a table for every system + a table for payment system descriptions linking to the name of the relevant table + 2 extra fields in the orders table: payment system id (will link to "description table"), payment record id (will link to the id in the relevant payment system table). This would also allow to provide specific functionality (order notifications processing etc.) to be separated among payment system models instead of using if
s. Is it ok?
If anything, the project is based on PHP 5, Doctrine and MySQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可以说,您可以有一个包含任何公共信息(ID、名称等)的主系统表,然后有一个收集属性的名称/值对的第二个表:
由于属性的模糊映射,这可能对人们没有吸引力但另一方面,获取任何系统的属性都变成了简单的联接,而不是试图将几个表拉到一起。
请注意,上面的 SQL 创建语句是伪代码。
Arguably you could have a main System table that contains any common information (an id, a name, whatever) and then a second table that collects name/value pairs for the attributes:
This might be unattractive to people because of the fuzzy mapping of properties but on the other hand getting the properties for any system becomes a simple join rather than trying to pull a handful of tables together.
Please note that the SQL create statements above are pseudocode-ish.
每种类型支付系统都有一个表 - 该类型是一个或多个支付系统共有的一组唯一属性。拥有所有这些类型的父表,其中包含所有类型共享的属性(例如系统名称)。
One table for each type of payment system - the type being a unique set of attributes common to one or more payment system. Have a parent table of all those types that contains attributes shared by all of them (for instance the system name).
dportas 和 jaydel 所说的话加上我的两分钱。
每个支付系统应该位于表的一行中,而不是位于其自己的表中。每种类型的支付系统(专用支付系统)都需要一个自己的表来存储仅针对该类型收集的数据。
此外,应该有一个通用支付系统表,其中包含与所有类型支付系统相关的数据。
通用支付系统表和专用支付系统表之间的关系是经典的“gen=spec 设计模式”。如果您查找“泛化专业化关系建模”,您会找到教您如何为 gen-spec 模式设计关系表的文章。
简而言之,gen 表有一个经典的 ID 字段作为 PK。每个专用表都有一个 ID 字段,该字段与 gen 表中的 ID 字段重复。这意味着它既是专业领域的PK,又是引用gen表的FK。
FK 对数据库中其他地方的支付系统的引用应引用 gen 表。
What dportas and jaydel said plus my two cents.
Each payment system should be in a row of a table, not in a table of its own. Each type of payment system (speacialized payment system) needs a table of its own for data that is only collected for that type.
In addition, there should be a generic payment system table containing data that pertains to all types of payment systems.
The relationship betwee the generic payment system table and the specialized payments system tables is a classic "gen=spec desing pattern". If you look up "generalization specialization relational modeling" you will find articles that teach you how to design relational tables for the gen-spec pattern.
The short answer is that the gen table has a classic ID field acting as a PK. Each specialized table has an ID field that is a duplicate of the ID field in the gen table. This means that it is both a PK in the specialized field and a FK referencing the gen table.
FK references to a payment system elsewhere in the database should reference the gen table.