外键约束失败,无法添加或更新子行:
a foreign key constraint fails, Cannot add or update a child row:
**我是 sql 的新手,我遇到了无法添加或更新子行的问题:外键约束失败如果有人可以指出错误,请指出,我没有知道它是什么。谢谢! sql Fiddle link
http://www.sqlfiddle.com/#!9/3b724/2
CREATE TABLE donut
(
donutid INT(5) NOT NULL,
name VARCHAR(20),
descriptioin VARCHAR(60),
price DECIMAL(8,3),
PRIMARY KEY (donutid)
);
CREATE TABLE customer
(
customerid INT(5)NOT NULL,
firstname VARCHAR(30),
lastname VARCHAR(30),
addres VARCHAR(100),
apt VARCHAR(20),
city VARCHAR(30),
state VARCHAR(20),
zip VARCHAR(8),
homephone VARCHAR(15),
cellphone VARCHAR(15),
otherphone VARCHAR(15),
PRIMARY KEY (customerid)
);
CREATE TABLE customer_order_donut
(
orderid INT(10) NOT NULL,
customerid INT(10),
donutid INT(10),
orderdate DATETIME,
donutqty INT(5),
specialhandling TEXT,
PRIMARY KEY (orderid),
FOREIGN KEY (customerid) REFERENCES customer(customerid),
FOREIGN KEY (donutid) REFERENCES donut(donutid)
);
INSERT INTO customer (customerid, firstname, lastname, addres, apt, city, state, zip, homephone,
cellphone, otherphone)VALUES ('786','Alfreds','John','1417 Gali Gondni','2T',
'Lawrenceville','GA','30046','14235678','12345678','14563287');
INSERT INTO customer (customerid, firstname, lastname, addres, apt, city, state, zip, homephone,
cellphone, otherphone)VALUES ('829','Thomas','Jawad','1917 Gondni St','3PG',
'Jackville','FL','32046','1344235678','14563287','345934522');
INSERT INTO customer (customerid, firstname, lastname, addres, apt, city, state, zip, homephone,
cellphone, otherphone)VALUES ('779','Jawad','Peter','19337 Kalan CT','34PD',
'Snailville','VA','45612','345934522','4525852','1455445452');
INSERT INTO donut (donutid, name, descriptioin, price) VALUES ('101','Yeast',' Light Airy ','1.20');
INSERT INTO donut (donutid, name, descriptioin, price) VALUES ('111','Cake ',' Bit Crustier ','2.40');
INSERT INTO donut (donutid, name, descriptioin, price) VALUES ('121','Old-Fashioned',' More Crunch','3.80');
CREATE INDEX Donut_name_feild
ON donut (name);
INSERT INTO customer_order_donut (orderid, customerid, donutid, orderdate,donutqty,specialhandling) VALUES ('141','101','251','2010-10-11','14','In Rush');
INSERT INTO customer_order_donut (orderid, customerid, donutid, orderdate,donutqty,specialhandling) VALUES ('151','111','326','2010-10-61','12','No Coffe');
INSERT INTO customer_order_donut (orderid, customerid, donutid, orderdate,donutqty,specialhandling) VALUES ('161','121','235','2010-10-91','15','In No Hurry');
您缺少客户 table 中 customer_id 101 的密钥。
INSERT INTO customer_order_donut (orderid, customerid, donutid, orderdate,donutqty,specialhandling) VALUES ('141','101','251','2010-10-11','14' ,'In Rush');
**我是 sql 的新手,我遇到了无法添加或更新子行的问题:外键约束失败如果有人可以指出错误,请指出,我没有知道它是什么。谢谢! sql Fiddle link http://www.sqlfiddle.com/#!9/3b724/2
CREATE TABLE donut
(
donutid INT(5) NOT NULL,
name VARCHAR(20),
descriptioin VARCHAR(60),
price DECIMAL(8,3),
PRIMARY KEY (donutid)
);
CREATE TABLE customer
(
customerid INT(5)NOT NULL,
firstname VARCHAR(30),
lastname VARCHAR(30),
addres VARCHAR(100),
apt VARCHAR(20),
city VARCHAR(30),
state VARCHAR(20),
zip VARCHAR(8),
homephone VARCHAR(15),
cellphone VARCHAR(15),
otherphone VARCHAR(15),
PRIMARY KEY (customerid)
);
CREATE TABLE customer_order_donut
(
orderid INT(10) NOT NULL,
customerid INT(10),
donutid INT(10),
orderdate DATETIME,
donutqty INT(5),
specialhandling TEXT,
PRIMARY KEY (orderid),
FOREIGN KEY (customerid) REFERENCES customer(customerid),
FOREIGN KEY (donutid) REFERENCES donut(donutid)
);
INSERT INTO customer (customerid, firstname, lastname, addres, apt, city, state, zip, homephone,
cellphone, otherphone)VALUES ('786','Alfreds','John','1417 Gali Gondni','2T',
'Lawrenceville','GA','30046','14235678','12345678','14563287');
INSERT INTO customer (customerid, firstname, lastname, addres, apt, city, state, zip, homephone,
cellphone, otherphone)VALUES ('829','Thomas','Jawad','1917 Gondni St','3PG',
'Jackville','FL','32046','1344235678','14563287','345934522');
INSERT INTO customer (customerid, firstname, lastname, addres, apt, city, state, zip, homephone,
cellphone, otherphone)VALUES ('779','Jawad','Peter','19337 Kalan CT','34PD',
'Snailville','VA','45612','345934522','4525852','1455445452');
INSERT INTO donut (donutid, name, descriptioin, price) VALUES ('101','Yeast',' Light Airy ','1.20');
INSERT INTO donut (donutid, name, descriptioin, price) VALUES ('111','Cake ',' Bit Crustier ','2.40');
INSERT INTO donut (donutid, name, descriptioin, price) VALUES ('121','Old-Fashioned',' More Crunch','3.80');
CREATE INDEX Donut_name_feild
ON donut (name);
INSERT INTO customer_order_donut (orderid, customerid, donutid, orderdate,donutqty,specialhandling) VALUES ('141','101','251','2010-10-11','14','In Rush');
INSERT INTO customer_order_donut (orderid, customerid, donutid, orderdate,donutqty,specialhandling) VALUES ('151','111','326','2010-10-61','12','No Coffe');
INSERT INTO customer_order_donut (orderid, customerid, donutid, orderdate,donutqty,specialhandling) VALUES ('161','121','235','2010-10-91','15','In No Hurry');
您缺少客户 table 中 customer_id 101 的密钥。
INSERT INTO customer_order_donut (orderid, customerid, donutid, orderdate,donutqty,specialhandling) VALUES ('141','101','251','2010-10-11','14' ,'In Rush');