获得重复的主键错误

时间:2016-11-28 23:29:43

标签: mysql sql

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'),

1 个答案:

答案 0 :(得分:2)

您需要一个新表,其中包含REQUEST和ITEMS之间的多对多关系

例如表REQUEST和ITEMS

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);

我希望这会有所帮助 的问候,