简体   繁体   中英

Two tables with m to n relationship

I have two tables, Puchases( purchID ,empID*(foreignkey)*) and Item( itemID ,itemName,unitPrice). The two tables have to have a many to many relationship. I can insert data into the two tables separately but inserting data to the table bought( purchID,itemID ,qty) is not possible. Which SQL statement should I use?

You need a new table:

CREATE TABLE Purchase_Item (
    purchID INT, -- foreign key to Purchases.purchID
    itemID INT, -- foreign key to Item.itemID
    quantity INT,
    PRIMARY KEY (purchID, itemID)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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