【发布时间】:2012-04-28 07:24:43
【问题描述】:
我正在构建一个包含以下业务逻辑的发票应用程序。
a) 为客户下一个新订单。 (一个订单是一组三个 相关组件、估价、发票和采购订单)
b) 下订单后,可以生成新的估价。订单 将只有一个估算值。(估算值包含项目详细信息)
c) 参考订单的估计。发票可以是 生成。发票有资格享受价格折扣。除了 项目详细信息,发票包含一些费用。订单可以包含 只有一张发票
d) 参考订单的发票,PurchaseOrder 可以 生成。 PurchaseOrder 包含有关供应商的项目信息 购买。一个订单可以包含多个 PurchaseOrder。
这是我想出的数据库表设计。
虽然看起来都不错,但我很难决定将属于特定估算、发票或采购订单的项目列表存储在哪里。
我想到了几个解决方案。
方法A:为每个项目列表创建不同的表格。 (估价、发票和采购订单)
表格:estimate_item、invoice_item、purchaseorder_item。(此表格包含与 上图中的 order_item)。
问题: 这种方法的问题是所有三个表都由存储相同信息的相同列组成,唯一的 不同的是要存储的外键。方法 B: 创建一项列表表
order_item
表名:order_item
问题:不确定在这个表中存储什么作为外键,因为外键可以来自三个不同的表。我想到了几个 该表中外键的处理方式如下。1)foreignKey 表参考列: 类型(示例值:估价、发票、采购订单) foreignKey 列:type_id(consist 三个表中任意一个的 foreignKey)
问题: 我正在使用命名 列名的约定例如以tablename_id结尾的列名定义外键。而且这种方法违反了规则。2) foreignKeyColumn:
order_id,estimate_id,invoice_id,purchaseorder_id.
问题: 不必要的外键列 已定义。
我想知道我应该如何将外键存储在order_item 表中,以便它识别它所属的订单和估计/发票/采购订单。
表的关系是:
id是所有表的主键
table name: order relates to (contact, estimate, invoice, shipment) tables.
column name: contact_id (foreign key(referring to id column of the contact table)).
column name: estimate_id (foreign key(referring to id column of the estimate table)).
column name: invoice_id (foreign key(referring to id column of the invoice table)).
column name: shipment_id (foreign key(referring to id column of the shipment table)).
tablename: purchaseorder (this have one to many relationship with order table)
column name: order_id (foreign key(referring to id column of the order table)).
column name: contact_id (foreign key(referring to id column of the contact table)).
问题是关于如何在 order_item 表中存储外键。
谢谢。
更新 1:
请注意,每个表 estimate 、 invoice 和 purchaseorder 都有自己的项目并且彼此没有关系。
【问题讨论】:
-
肯定
order_item引用了关联的order,而后者又引用了关联的invoice、estimate和/或purchaseorder?为什么需要从每个order_item中引用后面的表? -
@eggyal 对不起,我上传了错误的图片。这只是我正在进行的思考过程。并使用所有字段更新列。现在更新了我的图片
-
但我的观点仍然存在...您有多个
order_item引用每个order,而这些invoice、estimate和/或purchaseorder又相应地引用关联,不是吗?有什么问题(ERD 中的箭头除外)? -
是的,每个订单由多个项目组成,因此包含多个 order_item,我必须将
order_item与invoice、estimate、purchaseorder相关联,而这又属于一个订单。跨度> -
我觉得我们在绕圈子。我的第一个问题是“为什么需要从每个
order_item中引用后面的表 [假设order_item引用了order,而order又引用了这些表]”?您是否建议 same 顺序中的项目可能与 differentinvoice、estimate或purchaseorder相关?我认为您正在尝试解决一个不存在的问题。
标签: mysql database-design relational-database