在发票表和该发票中的项目表之间建立良好关系的最佳方法是什么?
所以我有发票数据和发票中项目的数据,我的问题是,发票和项目表的数据库结构的最佳方法是什么。
我看到的方式有两种。
示例:
发票表:
+------------+-------------+-----+--------------+
| 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表)的主键
发票表:
+------------+-------------+-----+--------------+
| 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 |
+------------+------------+---------------+-----+-------------+
有更好的方法吗?