我有三个规范化的表:
+---------------+
|Product |
+---------------+
|productCode(PK)|
|title |
|description |
|price |
|productGroup |
+---------------+
m
|
|
1
+------------------+
|Purchase Info |
+------------------+
|productCode(FK) |
|purchaseNumber(FK)|
|offerPrice |
|quantity |
+------------------+
1
|
|
m
+---------------------+
|Purchase | +-----------------+
+---------------------+ |Branch |
|purchaseNumber(PK) | +-----------------+
|productCode(FK) | |branchNumber(PK) | more
|branchNumber(FK) | m-----0 |buildingName | x2 (salesPerson,
|salesPersonNumber(FK)| |city | accountNo)
|accountNumber(FK) | |postcode | similar tables
|time | |telNumber |
|tillNo | +-----------------+
+---------------------+
1 = one
0 = many
但我不太确定这些现在如何起到'数据库'的作用。在此规范化之前,purchaseInfo
表不存在,属性属于purchase
表。 但 product
和purchase
是多对多的关系。
我想要的是让用户能够使用以下字段添加新购买:
productCode
quantity
offerPrice
并且能够从其他每个表中获取内容:
我如何设置MySQL查询?
我想到了这样的事情:
INSERT into `purchaseInfo` (productCode, purchaseNumber, offerPrice, quantity)
VALUES ('".$productCode."', '".$purchaseNumber."', '".$offerPrice."', '".$quantity."');
但这对我来说并不合理。
关于我实际在做什么的任何其他想法或解释?对不起,我受过非常严重的教育,并且不太确定发生了什么!
答案 0 :(得分:0)
购买中的每条记录都像是在超市买的一张支票(例如10瓶喜力啤酒和2条面包)
购买信息中的每条记录都是该支票上的一行(例如10瓶喜力啤酒)
产品中的每条记录都是一种产品(例如"喜力")
因此,当您进行购买时,您可能至少需要两次插入 1)插入购买 2)插入到PURCHASE_INFO中 - 与您在一次购买中使用不同产品的次数相同
另一个主题:插入时不应连接字符串。使用参数化查询(带?标记)代替。