Comoresolver error ERROR 1452 (23000) in mysql

-1

I'm trying to popular the following database:

DROP DATABASE IF EXISTS library;

CREATE DATABASE library;

USE library;

CREATE TABLE AUTHOR (
  NAME VARCHAR(50) NOT NULL,
  COUNTRY VARCHAR(50) NOT NULL,
  TITLE VARCHAR(50) NOT NULL,
  PRIMARY KEY (NAME,TITLE)
) ENGINE = INNODB;

CREATE TABLE BOOK (
  TITLE VARCHAR(50) NOT NULL,
  ISBN CHAR(10) NOT NULL,
  PUBLISHER VARCHAR(50) NOT NULL,
  SUBJECT VARCHAR(50) NOT NULL,
  PRIMARY KEY (ISBN)
) ENGINE = INNODB;

CREATE TABLE WRITES (
  NAME VARCHAR(50) NOT NULL,
  TITLE VARCHAR(50) NOT NULL,
  ISBN CHAR(10) NOT NULL,
  PRIMARY KEY (NAME,ISBN)
) ENGINE = INNODB;

ALTER TABLE WRITES ADD CONSTRAINT FK_BOOK_WRITES
  FOREIGN KEY(ISBN) REFERENCES BOOK(ISBN)
  ON DELETE RESTRICT ON UPDATE RESTRICT;

ALTER TABLE WRITES ADD CONSTRAINT FK_AUTHOR_WRITES
  FOREIGN KEY(NAME) REFERENCES AUTHOR(NAME)
  ON DELETE RESTRICT ON UPDATE RESTRICT;

INSERT INTO AUTHOR (NAME,COUNTRY,TITLE) VALUES("Dorothy King","England","Springtime Gardening");
INSERT INTO AUTHOR (NAME,COUNTRY,TITLE) VALUES("Jon Nkoma","Kenya","Birds of Africa");
INSERT INTO AUTHOR (NAME,COUNTRY,TITLE) VALUES("Won Lau","China","Early Tangs Paintings");
INSERT INTO AUTHOR (NAME,COUNTRY,TITLE) VALUES("Bert Kovalsco","United States","Baskets for Today");
INSERT INTO AUTHOR (NAME,COUNTRY,TITLE) VALUES("Tom Quercos","Mexico","Mayan Art");
INSERT INTO AUTHOR (NAME,COUNTRY,TITLE) VALUES("Jimmy Chan","China","Early Tangs Paintings");
INSERT INTO AUTHOR (NAME,COUNTRY,TITLE) VALUES("Dorothy King","England","Autumn Annuals");
INSERT INTO AUTHOR (NAME,COUNTRY,TITLE) VALUES("Jane East","United States","Springtime Gardening");

INSERT INTO BOOK (TITLE,ISBN,PUBLISHER,SUBJECT) VALUES("Springtime Gardening","0816354219","Harding","Nature");
INSERT INTO BOOK (TITLE,ISBN,PUBLISHER,SUBJECT) VALUES("Early Tangs Paintings","036487547X","Ballman","Art");
INSERT INTO BOOK (TITLE,ISBN,PUBLISHER,SUBJECT) VALUES("Birds of Africa","0115012141","Loraine","Nature");
INSERT INTO BOOK (TITLE,ISBN,PUBLISHER,SUBJECT) VALUES("Springtime Gardening","0560001428","Swift-Key","Nature");
INSERT INTO BOOK (TITLE,ISBN,PUBLISHER,SUBJECT) VALUES("Baskets for Today","0816537054","Harding","Art");
INSERT INTO BOOK (TITLE,ISBN,PUBLISHER,SUBJECT) VALUES("Autumn Annuals","0816885060","Harding","Nature");

INSERT INTO WRITES (NAME,TITLE,ISBN) VALUES("Jimmy Chan","Early Tangs Paintings","036487547X");
INSERT INTO WRITES (NAME,TITLE,ISBN) VALUES("Dorothy King","Autumn Annuals","0816885060");
INSERT INTO WRITES (NAME,TITLE,ISBN) VALUES("Jane East","Springtime Gardening",0560001428);
INSERT INTO WRITES (NAME,TITLE,ISBN) VALUES("Bert Kovalsco","Baskets for Today","0816537054");
INSERT INTO WRITES (NAME,TITLE,ISBN) VALUES("Won Lau","Early Tangs Paintings","036487547X");
INSERT INTO WRITES (NAME,TITLE,ISBN) VALUES("Jon Nkoma","Birds of Africa","0115012141");
INSERT INTO WRITES (NAME,TITLE,ISBN) VALUES("Dorothy King","Springtime Gardening","0816354219");

But I get the following error:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails ('library'.'WRITES', CONSTRAINT 'FK_BOOK_WRITES' FOREIGN KEY ('ISBN') REFERENCES 'BOOK' ('ISBN'))

There's already a similar question a>, but it did not help me much.

    
asked by anonymous 26.03.2018 / 17:05

1 answer

0

In fact there is a "gambiarra" that you can do, but if there is constraint error your data will not be correct regarding the foreign key, but to remedy your problem and you can at least enter your data, check foreign key at the beginning of your script and enable it again at the end, in this way.

Disable: SET FOREIGN_KEY_CHECKS = 0;

Enable: SET FOREIGN_KEY_CHECKS = 1;

I do not advise doing this, but it solves your problem by the hour.

    
28.03.2018 / 15:28