我正在尝试将文本文件中的数据插入到具有外键的表中,而另一个表已经加载了数据。出于某种原因,虽然我似乎无法在这个新表中插入任何内容。
这是我用来创建表格的代码:
CREATE TABLE GENRE
(
genre_name varchar(255) NOT NULL,
PRIMARY KEY (genre_name)
);
CREATE TABLE BOOK
(
isbn int NOT NULL,
title varchar(255) NOT NULL,
price DECIMAL(11,2) NOT NULL,
genre_nm varchar(255) NOT NULL,
PRIMARY KEY (isbn),
CONSTRAINT fk_BOOK FOREIGN KEY (genre_nm)
REFERENCES GENRE(genre_name)
);
然后我的INSERT语句:
INSERT INTO BOOK
-> (isbn,title,genre_nm,price)
-> VALUES
-> (582394706,'I Shall Be Near to You: A Novel','drama',25.31),
-> (199978896,'A Breath of Frost ','fiction',41.99),
-> (506705707,'The Death Class: A True Story About Life','psychology',25.99),
-> (582553303,'The Walking Dead','comics',18.67),
-> (780701543,'Anna Karenina','drama',29.99),
-> (213261709,'A bad day','drama',14.98),
-> (457697612,'Believe in yourself','psychology',97.12),
-> (99897086,'A place behind the pines','fiction',24.23),
-> (729326086,'Near empty','comics',9.99);
错误:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`homework`.`book`, CONSTRAINT `book_ibfk_1` FOREIGN KEY (`genre_nm`) REFERENCES `GENRE`
(`genre_name`))
任何帮助将不胜感激!谢谢!
mysql> SELECT * FROM GENRE;
+-------------+
| genre_name |
+-------------+
| comics |
| drama |
| fiction |
| psychology |
+-------------+
4 rows in set (0.00 sec –