1

我正在为一家餐馆制作一个 mysql 数据库。

我有一个表叫做:*tbl_contents*它存储了用于准备不同菜单项的所有内容。
现在我必须为所有支出维护一张表。这些支出可以是“购买内容”或一些常规支出,如电费或餐厅租金。

如何将两种支出存储在同一张表中?

我有桌子tbl_fixed_expanditurestbl_contents。如果我为厨房买了一些东西,它应该存放在里面tbl_contents,如果我已经支付了电费,它就会被保存在tbl_fixed_expenditures.

在此处输入图像描述

4

2 回答 2

1

嗯,很难给出一个正确的答案,但我可以根据我目前的理解给你一些模糊的猜想。

如果这两种支出具有不同的属性,但它们有一些共同点,则应该对表格进行规范化。使用expenditures_tranx作为中间表(就像,在 OO 术语中,顶级类)和剩余的表tbl_fixed_expanditures,并且tbl_contents可以是“专用”表(再次,在 OO 术语中,那些将从父级“继承”属性的表表),它将存储有关支出的更详细信息。这是一个简单的模型实体关系草案来说明。

   ____________          ___________________        _______________________    
  |tbl_contents|-1----*-| expenditures_tranx|-*---1-|tbl_fixed_expenditures|
  |exp_id:fk___|        |___________________|       |exp_id:fk_____________|

这是一篇解释这些概念的有趣文章:http: //apps.topcoder.com/wiki/display/training/Entity+Relationship+Modeling

Let me know what you think.

于 2012-12-27T18:09:32.433 回答
1

You are essentially trying to represent inheritance in a relational database.

You have two "classes" which are similar in some ways, and different in others. My suggestion is to create a table to act as a parent to both tbl_expanditures and tbl_fixed_expanditures.

Here's what I would do:

+------------------+
| tbl_expenditures |
+------------------+
| id               |
+------------------+

+------------------------+
| tbl_fixed_expenditures |
+------------------------+
| id                     |
| expenditureId          |
| ...                    |
+------------------------+

+---------------------------+
| tbl_variable_expenditures |
+---------------------------+
| id                        |
| expenditureId             |
| ...                       |
+---------------------------+

...where tbl_fixed_expenditures.expenditureId and tbl_variable_expenditures.expenditureId both have a reference to tbl_expenditures.id.

This way, when you need to refer to them simply as "expenditures" (for example, in your transaction table), you can reference tbl_expenditures, and when you need information that is unique to either fixed or variable expenditures, you can refer to the "child" tables.

这是关系数据库中非常常见的问题,有多种处理方法,每种方法各有利弊。IBM 有一篇非常好的文章概述了这些选项,我强烈推荐它进一步阅读:

http://www.ibm.com/developerworks/library/ws-mapping-to-rdb/

于 2012-12-27T18:10:37.220 回答