2016-11-27 87 views
-1

我想在SQL脚本中建立两个表之间的关系,但是每次运行这个时都会出现错误1452。我不知道为什么&已经尝试了一切。 我的库存表中有“描述”作为客户表中的外键。 “说明”是库存表中的主键。我究竟做错了什么。我对SQL很陌生,所以请原谅我的无知,如果这确实是一个简单的错误。错误1452 SQL WorkBench

感谢,

DROP DATABASE IF EXISTS dunnes; 
create database dunnes; 
USE dunnes; 


DROP table IF exists CUSTOMERS; 
DROP table if exists MOVIES; 
DROP table if exists STOCK; 
DROP table if exists TRANSACTIONS; 




CREATE TABLE MOVIES(
genre varchar(10) not null, 
title varchar (25) not null, 
released NUMERIC (4), 
actor varchar (30) Not null, 
movie_Id varchar (6) PRIMARY KEY); 


create table STOCK(
item_No varchar(5) NOT NULL, 
cost numeric (4,2) DEFAULT 00.00 NOT NULL, 
description varchar (30) PRIMARY KEY, 
quantity numeric (10) not null); 






CREATE table TRANSACTIONS (
transaction_Id NUMERIC (4) PRIMARY KEY, 
trans_Date DATE NOT NULL, 
return_Date DATE NOT NULL, 
cost numeric (4,2) DEFAULT 00.00 not null, 
movie_Id varchar (6), 
title varchar (25) not null, 
index (movie_Id), 
CONSTRAINT FK_TR_movie_Id foreign key (movie_Id) references MOVIES(movie_Id) 
); 




CREATE table CUSTOMERS (
cust_Num NUMERIC (4) PRIMARY KEY, 
cust_Fname VARCHAR (20) NOT NULL, 
cust_Sname VARCHAR (20) NOT NULL, 
cust_address VARCHAR (50) NOT NULL, 
cust_dob DATE NOT NULL, 
cust_membership VARCHAR (10) NOT NULL, 
description varchar (30) NOT NULL, 
cost numeric (4,2) DEFAULT 00.00 NOT NULL, 
index (description), 
CONSTRAINT FK_TR_description foreign key (description) references STOCK(description) 
); 








INSERT INTO customers VALUES (001, "Denise" , "Geoghegan" , "1 Main Street, Galway" , "1990-05-22", "Active", "King Crisps", "0.99"); 
INSERT INTO customers VALUES (002, "John", "Doe", "Apartment 17, Sandyford, Dublin", "1992-06-01", "Active", "Pringles - Small","1.50"); 
INSERT INTO customers VALUES (003, "Rebecca", "Smyth", "13 Hillcrest, Lucan, Co. Dublin", "1989-10-21", "Expired", "Pringles - Large", "2.50"); 
INSERT INTO customers VALUES (004, "Ronan", "O'Neill", "250 Navan Road, Dublin 15", "1985-11-22", "Active", "Mars Bar", "00.80"); 
INSERT INTO customers VALUES (005, "Todd", "Richards", "5 Brookfield Lodge, Maynooth, Co. Kildare", "1995-05-17", "Active", "Twix Bar", "00.80"); 
INSERT INTO customers VALUES (006, "Eileen", "Llyod", "15 Seaview, Camolin, Co. Wexford", "1985-03-02", "Active","Can - Coke", "01.10"); 
INSERT INTO customers VALUES (007, "Siobhan", "Coonan", "15 South Main Street, Howth , Co. Dublin", "1975-07-22", "Expired", "Can - Club Orange", "01.10"); 
INSERT INTO customers VALUES (008, "Ollie", "Ryan", "18 Ryebridge, Celbridge. Co. Kildare", "1994-05-22", "Active", "Can - 7up", "01.10"); 
INSERT INTO customers VALUES (009, "Nuala", "McLoughlin", "151 Richmond Hill, Howth, Co. Dublin", "1987-06-12", "Expired", "Popcorn - Small", "00.99"); 
INSERT INTO customers VALUES (010, "Richard", "Finn", "115 Newtown Hall, Kilcock, Co. Wexford", "1985-12-25", "Expired", "Popcorn - SmalL", "1.99"); 
commit; 
commit; 


insert into movies values ("Action", "Rocky I", "1977", "Sylvester Stalone", "7701AM"); 
insert into movies values ("Romcom", "The Holiday", "2006", "Cameron Diaz", "0677RC"); 
insert into movies values ("Romcom", "Love Actually", "2003", "Hugh Grant", "0378RC"); 
insert into movies values ("Action", "Rocky II", "1979", "Sylvester Stalone", "7999AR"); 
insert into movies values ("Children", "Disney - Peter Pan", "1953", "Bobby Driscoll", "5366CM"); 
insert into movies values ("Children", "Disney - The Lion King", "1994", "Jeremy Irons", "9452CM"); 
insert into movies values ("Action", "Rocky III", "1982", "Sylvester Stalone", "8286AM"); 
insert into movies values ("Drama", "My Girl", "1991", "Anna Chlumsky", "9165DM"); 
insert into movies values ("Action", "Sherlock Holmes", "2009", "Robert Downey Jr.", "0942AM"); 
insert into movies values ("Drama", "Man on Fire", "2004", "Denzel Washington", "0412DM"); 
commit; 




insert into stock values ("A101", "00.99", "King Crisps", "100"); 
insert into stock values ("A102", "1.50", "Pringles Small", "77"); 
insert into stock values ("A103", "2.50", "Pringles Large", "24"); 
insert into stock values ("A104", "00.80", "Mars Bar", "112"); 
insert into stock values ("A105", "00.80", "Twix Bar", "40"); 
insert into stock values ("A106", "1.10", "Can - Coke", "30"); 
insert into stock values ("A107", "1.10", "Can - Club Orange", "55"); 
insert into stock values ("A108", "1.10", "Can - 7Up", "15"); 
insert into stock values ("A109", "00.99", "Popcorn - Small", "163"); 
insert into stock values ("A1010", "1.99", "Popcorn - Large", "44"); 
commit; 


INSERT INTO transactions values (2311, "2016-11-01", "2016-11-25", "2.50", "7701AM", "Rocky I"); 
INSERT INTO transactions values (2312, "2016-11-01", "2016-11-25", "2.50", "0677RC", "The Holiday"); 
INSERT INTO transactions values (2413, "2016-11-02", "2016-11-27", "5.00", "0378RC", "Love Actually"); 
INSERT INTO transactions values (2414, "2016-11-02", "2016-11-29", "2.50", "7999AR", "Rocky II"); 
INSERT INTO transactions values (2515, "2016-11-05", "2016-12-01", "2.50", "5366CM", "Disney - Peter Pan"); 
INSERT INTO transactions values (1016, "2016-11-10", "2016-12-05", "7.50", "9452CM", "Disney - The Lion King"); 
INSERT INTO transactions values (1517, "2016-11-05", "2016-12-08", "2.50", "8286AM", "Rocky III"); 
INSERT INTO transactions values (1011, "2016-11-1", "2016-12-12", "5.00", "9165DM", "My Girl"); 
INSERT INTO transactions values (1211, "2016-11-22", "2016-12-15", "2.50", "0942AM", "Sherlock Holmes"); 
INSERT INTO transactions values (2512, "2016-11-30", "2016-12-19", "5.50", "0412DM", "Man on Fire"); 
commit; 
select * from CUSTOMERS 

以下是完整的错误:

INSERT INTO customers VALUES (001, "Denise" , "Geoghegan" , "1 Main Street, Galway" , "1990-05-22", "Active", "King Crisps", "00.99") 

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (dunnes.customers, CONSTRAINT FK_TR_description FOREIGN KEY (description) REFERENCES stock (description))

+0

在插入到Customers之前,您需要插入股票表的数据,以便在外部引用的数据关键字存在,为什么你有一个从Customer.Description到Stocks.Description的外键是另一个问题,但它看起来很奇怪,但是在你的模型中也许是有道理的。 – jpw

回答

0

错误消息不言自明:你有你的customer表指示的description领域的外键到stock表中具有相同名称的字段。您尝试将数据插入customer表中,而您的stock表为空。

要么填充stock表第一或禁用外键检查(见this SO question on how to do this),或者完全删除该外键,因为它没有任何意义。

这是有道理的应用了大规模的前禁用外键检查sql脚本,你知道它只包含有效数据,但在这种情况下,我宁愿放弃这个外键

+0

谢谢@Shadow。正如你可能知道的,我是这个品牌的新手,所以我非常感谢你的帮助和建议 – ThisisD