2016-11-28 86 views
0
INSERT INTO REQUEST (OrderNumber, CustomerID, ItemID) 
VALUES ('10011', '10', '1001'), 
     ('10011', '10', '1007'), 
     ('10011', '10', '1014'), 
     ('10021', '05', '1008'), 
     ('10021', '05', '1014'), 
     ('10031', '07', '1014'), 
     ('10031', '07', '1019'), 
     ('10041', '31', '1015'), 
     ('10041', '31', '1007'), 
     ('10041', '31', '1024'), 
     ('10051', '50', '1014'), 
     ('10051', '50', '1004'), 
     ('10051', '50', '1011'), 
+0

你能分享'desc request'的輸出嗎? –

+1

哪些字段組成了PK? – Searching

+0

PK是訂單編號,從10011 –

回答

2

您需要保存的許多請求和項目之間的多對多關係的新表

例如,對於表請求和物品

CREATE TABLE REQUEST (
    OrderNumber Integer, 
    CustomerID Integer, 
    OrderDate  DateTime, 
    PRIMARY KEY (OrderNumber) 
); 

Insert INTO REQUEST values 
    (10011, 10, now()), 
    (10041, 31, now()); 

CREATE TABLE ITEMS (
    ItemID   Integer, 
    ItemDescription Varchar(50), 
    PRIMARY KEY (ItemID) 
); 

Insert into ITEMS values 
      (1001,'Screw'), 
      (1007,'Rod'), 
      (1015,'Nail'); 

這將是新的表

CREATE TABLE REQUEST_ITEMS (
    OrderNumber Integer, 
    ItemID  Integer, 
    OrderQuantity Integer, 
    PRIMARY KEY (OrderNumber, ItemID) 
); 

Insert into REQUEST_ITEMS values 
     (10011, 1001, 3), 
     (10011, 1007, 25); 

Insert into REQUEST_ITEMS values 
     (10041, 1015, 10), 
     (10041, 1007, 15); 

我希望這有助於 問候,