MySQL错误:1452 - 无法添加或更新子行

时间:2018-04-06 11:39:47

标签: mysql database

当我尝试INSERT到Items_Puchased(底线)时似乎发生错误。忽略所有评论。

  

错误代码:1452。无法添加或更新子行:外键   约束失败(shippingcontainers.items_purchased,CONSTRAINT   items_purchased_ibfk_2 FOREIGN KEY(容器)REFERENCES stock   (容器))

CREATE TABLE Customer (
customer_reference int UNIQUE AUTO_INCREMENT,
primary key (customer_reference),
forename VARCHAR(20), 
surname VARCHAR(20), 
contact VARCHAR(15), 
email VARCHAR(50), 
building VARCHAR(5), 
road VARCHAR(40), 
city VARCHAR(30), 
postcode VARCHAR(7), 
county VARCHAR(30));

CREATE TABLE Invoice (
invoice_reference int UNIQUE AUTO_INCREMENT,
customer_reference int UNIQUE,
primary key (invoice_reference),
foreign key (customer_reference) references Customer(customer_reference),
invoice_cost DECIMAL(20,2),
paid bit,
order_date DATETIME,
delivery_date DATE);

CREATE TABLE Stock (
container VARCHAR(10) UNIQUE NOT NULL DEFAULT 0,
primary key (container),
SBADNLon INT(4),
SBADNFel INT(4),
SBADNSou INT(4),
CHECK (container = ("SBADN-Lon" > 0, "SBADN-Fel" > 0, "SBADN-Sou" > 0)));

这只显示了3个可能的容器变体 每个属性都存储一个值,该值包含库存中可用模型的编号

CREATE TABLE Items_Purchased (
container_ordered int UNIQUE AUTO_INCREMENT,
invoice_reference int,
container VARCHAR(10) NOT NULL DEFAULT "None",
container_cost decimal(20,2) NULL,
container_size VARCHAR(6) NOT NULL,
colour VARCHAR(5) NOT NULL,
grade CHAR(1) NOT NULL,
depot VARCHAR(15) NOT NULL,
container_type VARCHAR(20) NOT NULL,
conditionn VARCHAR(4) NOT NULL,
primary key (container_ordered, container_size, colour, grade, depot, container_type, conditionn));
foreign key (invoice_reference) references Invoice (invoice_reference),
foreign key (container) references Stock (container),
foreign key (container_size) references Container_Size (container_size),
foreign key (colour) references Colour (colour),
foreign key (grade) references Grade (grade),
foreign key (depot) references Depot (depot),
foreign key (container_type) references Container_Type (container_type),
foreign key (conditionn) references Conditionn (conditionn));


CREATE TABLE Depot (
depot VARCHAR(15) NOT NULL,
container_ordered int,
primary key (depot),
foreign key (container_ordered) references Items_Purchased(container_ordered),
CHECK (depot = ("london","felixstowe","southampton")));

CREATE TABLE Container_Type (
container_type VARCHAR(20) NOT NULL,
container_ordered int,
primary key (container_type),
foreign key (container_ordered) references Items_Purchased(container_ordered),
CHECK (container_type = ("dry","inslated","refreigerated","open top","tunnel")));

CREATE TABLE Container_Size (
container_size VARCHAR(6) NOT NULL,
container_ordered int,
primary key (container_size),
foreign key (container_ordered) references Items_Purchased(container_ordered),
CHECK (container_size = ("small","medium","large")));

CREATE TABLE Colour (
colour VARCHAR(5) NOT NULL,
container_ordered int,
primary key (colour),
foreign key (container_ordered) references Items_Purchased(container_ordered),
CHECK (colour = ("black","green")));

CREATE TABLE Conditionn (
conditionn VARCHAR(4) NOT NULL,
container_ordered int,
primary key (conditionn),
foreign key (container_ordered) references Items_Purchased(container_ordered),
CHECK (conditionn = ("new","used")));

CREATE TABLE Grade (
grade CHAR(1) NOT NULL,
container_ordered int,
primary key (grade),
foreign key (container_ordered) references Items_Purchased(container_ordered),
CHECK (grade = ("a","b","c")));
I am unsure on why I am getting this error code, can anyone assist? It occurs when adding 


INSERT INTO Customer (
forename, surname, contact, email, building, road, city, postcode, county)
VALUES (
"james", "kelly", 07930317616, "james@uni.com", 123, "Yellow Road", "Ipswich", "IP11SQ", "Suffolk");

INSERT INTO Customer (
forename, surname, contact, email, building, road, city, postcode, county)
VALUES (
"ben", "smith", 0793031754, "ben@uni.com", 45, "Red Road", "Woodbridge", "IP142DD", "Suffolk");

INSERT INTO Invoice (
invoice_cost, paid, order_date, delivery_date)
VALUES (
1435.34, 1, 19/12/2017, 21/12/2017);


INSERT INTO Invoice (
invoice_cost, paid, order_date, delivery_date)
VALUES (
1035.12, 0, 02/02/2018, 29/12/2017);

INSERT INTO Stock (
SBADNLon, SBADNFel, SBADNSou)
VALUES (
3, 2, 1);


INSERT INTO Items_Purchased (
container_cost, container_size, colour, grade, depot, container_type, conditionn)
VALUES (
1645.21, "large", "black", "a", "london", "insulated", "new")

2 个答案:

答案 0 :(得分:0)

查看与Items_Purchased表的外键关系。听起来你发生了不匹配的事情。在查看表定义时,您会识别出许多外键。这种类型的蜘蛛网构造不典型,可能会导致许多问题。我还要再看看你的外键并评估每个外键的需求。

答案 1 :(得分:0)

列容器的表库存中的默认值为0,Items_Purchased表中该列的默认值为“None”。 由于您没有为insert语句中的这些列分配任何值,因此当您尝试插入Items_Purchased表时,它使用列容器“None”的默认值,并且此值不存在于stock表中(唯一存在的值是0)。

您应该在Items_Purchased表中将它们设置为相同的值或默认值,将其设置为0,如:

CREATE TABLE Items_Purchased (
    container_ordered int UNIQUE AUTO_INCREMENT,
    invoice_reference int,
    container VARCHAR(10) NOT NULL DEFAULT 0,
    container_cost decimal(20,2) NULL,
    container_size VARCHAR(6) NOT NULL,
    colour VARCHAR(5) NOT NULL,
    grade CHAR(1) NOT NULL,
    depot VARCHAR(15) NOT NULL,
    container_type VARCHAR(20) NOT NULL,
    conditionn VARCHAR(4) NOT NULL,
    primary key (container_ordered, container_size, colour, grade, depot, container_type, conditionn));
    foreign key (invoice_reference) references Invoice (invoice_reference),
    foreign key (container) references Stock (container),
    foreign key (container_size) references Container_Size (container_size),
    foreign key (colour) references Colour (colour),
    foreign key (grade) references Grade (grade),
    foreign key (depot) references Depot (depot),
    foreign key (container_type) references Container_Type (container_type),
    foreign key (conditionn) references Conditionn (conditionn));