在发票表和该发票中的项目表之间建立良好关系的最佳方法是什么?

时间:2019-07-11 10:17:49

标签: mysql database entity-framework

在发票表和该发票中的项目表之间建立良好关系的最佳方法是什么?

所以我有发票数据和发票中项目的数据,我的问题是,发票和项目表的数据库结构的最佳方法是什么。

我看到的方式有两种。

  1. 要有2个表,一个表用于发票明细,第二个表用于与所有发票相关的所有项目。

示例:

发票表:

+------------+-------------+-----+--------------+
| Invoice ID | Items Total | Tax | Total Amount |
+------------+-------------+-----+--------------+
| 001        | 200         | 10  | 210          |
+------------+-------------+-----+--------------+
| 002        | 300         | 15  | 315          |
+------------+-------------+-----+--------------+
| 003        | 50          | 2.5 | 52.5         |
+------------+-------------+-----+--------------+

项目表:

+------------+------------+---------------+-----+-------------+
| Invoice ID | Product ID | Product Price | Qnt | Total Price |
+------------+------------+---------------+-----+-------------+
| 001        | #123450    | 50            | 3   | 150         |
+------------+------------+---------------+-----+-------------+
| 001        | #124556    | 10            | 5   | 50          |
+------------+------------+---------------+-----+-------------+
| 002        | #1254      | 25            | 2   | 50          |
+------------+------------+---------------+-----+-------------+
| 002        | #73450     | 100           | 1   | 100         |
+------------+------------+---------------+-----+-------------+
| 002        | #623450    | 40            | 3   | 120         |
+------------+------------+---------------+-----+-------------+
| 002        | #823450    | 6             | 5   | 30          |
+------------+------------+---------------+-----+-------------+
| 003        | #123450    | 50            | 1   | 50          |
+------------+------------+---------------+-----+-------------+

通过这种方法,当您想获取发票002的详细信息时,只需在项目表中选择所有发票ID为002的数据。

我发现这种方式是一个很好的示例,为什么有时您不需要表(Items表)的主键


  1. 具有1个发票表和每个发票项目表。 示例:

发票表:

+------------+-------------+-----+--------------+
| Invoice ID | Items Total | Tax | Total Amount |
+------------+-------------+-----+--------------+
| 001        | 200         | 10  | 210          |
+------------+-------------+-----+--------------+
| 002        | 300         | 15  | 315          |
+------------+-------------+-----+--------------+
| 003        | 50          | 2.5 | 52.5         |
+------------+-------------+-----+--------------+

项目表: 1

+------------+------------+---------------+-----+-------------+
| Invoice ID | Product ID | Product Price | Qnt | Total Price |
+------------+------------+---------------+-----+-------------+
| 001        | #123450    | 50            | 3   | 150         |
+------------+------------+---------------+-----+-------------+
| 001        | #124556    | 10            | 5   | 50          |
+------------+------------+---------------+-----+-------------+

2

+------------+------------+---------------+-----+-------------+
| Invoice ID | Product ID | Product Price | Qnt | Total Price |
+------------+------------+---------------+-----+-------------+
| 002        | #1254      | 25            | 2   | 50          |
+------------+------------+---------------+-----+-------------+
| 002        | #73450     | 100           | 1   | 100         |
+------------+------------+---------------+-----+-------------+
| 002        | #623450    | 40            | 3   | 120         |
+------------+------------+---------------+-----+-------------+
| 002        | #823450    | 6             | 5   | 30          |
+------------+------------+---------------+-----+-------------+

3

+------------+------------+---------------+-----+-------------+
| Invoice ID | Product ID | Product Price | Qnt | Total Price |
+------------+------------+---------------+-----+-------------+
| 003        | #123450    | 50            | 1   | 50          |
+------------+------------+---------------+-----+-------------+

有更好的方法吗?

0 个答案:

没有答案