简体   繁体   English

困在mysql数据库中的多对多关系

[英]Stuck at many-to-many relationship in mysql database

I am doing a simple Shopping application as my project. 我正在做一个简单的购物应用程序作为我的项目。 I am stuck to maintain a many-to-many relationship. 我坚持要保持多对多的关系。

I have to store all the details of a user transaction in some tables with relationship. 我必须在一些有关系的表中存储用户事务的所有细节。 Here are some important columns. 这是一些重要的专栏。

User_Id -- Foreign key from user table
Associated_Product_Id -- Foreign key from product table
Associated_Product_Quantity -- Not Null

For more simplicity 为了更简单

user1         product1         5 pieces
user1         product2         2 pieces
user1         product3         1 piece
user2         product1         2 pieces
user2         product3         3 pieces

Here one user can buy many products and one product can be bought by many users. 在这里,一个用户可以购买许多产品,并且许多用户可以购买一种产品。 So I am totally confused, how to manage this table. 所以我很困惑,如何管理这个表。 I mean which one is primary key here. 我的意思是哪一个是主键。

It doesn't matter in how many tables I will manages these information. 我将管理这些信息的表数无关紧要。 But I need to manage thes information. 但我需要管理这些信息。 Can anybody give a rough idea on managing this. 任何人都可以粗略地管理这个问题。 So that I could work on that direction. 这样我才能朝那个方向努力。

Thanks. 谢谢。

For the transaction you just need 1 table - something like you just posted. 对于交易,您只需要1个表 - 就像您刚刚发布的那样。

If you need to know what products a certain user bought, you query this table for User_Id , and if you need to know by what users a certain product was bought, you query the table for Associated_Product_Id . 如果您需要知道某个用户购买了哪些产品,您可以在此表中查询User_Id ,如果您需要知道某个产品的购买者是哪个用户,则可以在表中查询Associated_Product_Id (I assume that you already have your table for producs and users, since you are referencing the IDs here.) (我假设你已经拥有了产品和用户的表,因为你在这里引用了ID。)

You should of course add a column Transaction_Id or something similar, to uniquely identify a transaction. 您当然应该添加一列Transaction_Id或类似的东西,以唯一地标识一个事务。

Since you are doing a shopping application as a project, you may want to consider adding a fourth table into your schema, to hold Orders. 由于您正在将购物应用程序作为项目进行,因此您可能需要考虑在架构中添加第四个表以保存订单。

Your tables might then look something like: 您的表可能看起来像:

Users
-----
UserID (PK)
... (Other user details)

Product
-------
ProductID (PK)
... (Other product details)

Order
-----
OrderID (PK)
UserID (FK)
OrderDate

OrderLine
---------
OrderID (PK)
ProductID (PK)
OrderQuantity

This would then enable you to see how many units of a product a user bought on any given date, rather than just how many units that user has ever bought. 这样,您就可以查看用户在任何给定日期购买的产品的数量,而不是用户购买了多少单位。

You'll want three tables: 你需要三个表:

1) User Table 2) Product Table 3) Transaction Table 1)用户表2)产品表3)交易表

User 1:m Transaction Transaction 1:m Product 用户1:m交易交易1:m产品

Transaction will need keys to link both User & Product 交易将需要键来链接用户和产品

MySQL doesn't let you use many-to-many relationships in only two tables. MySQL不允许您仅在两个表中使用多对多关系。 You have to create a third table for the two tables you want to relate to. 您必须为要关联的两个表创建第三个表。

The third table will simply contain foreign keys for the products and users (plus an ID for each transaction). 第三个表将只包含产品和用户的外键(以及每个事务的ID)。

A simple example woulc be: 一个简单的例子是:

CREATE TABLE Product_User (
    trans_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    product_id INT NOT NULL, 
    user_id INT NOT NULL, 
    FOREIGN KEY (product_id) REFERENCES Product_table (productid), 
    FOREIGN KEY (user_id) REFERENCES User_table (userid)
);

With the corresponding names for the tables and the id fields. 使用表和id字段的相应名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM