Delete from Many to Many Relationship in MySQL

佐手、 提交于 2019-12-13 04:29:49

问题


I am not a MySQL professional I am just trying to make simple java application which accesses a MySQL database (e.g. The famous books/authors database) and manipulates it (i.e, add, update, delete).

The problem is that there are two tables that have many to many relationship (e.g. authors table and books table) when I am trying to delete using

DELETE FROM tableName WHERE someColumnName = 'someValue'

it throws MySQLIntegrityConstraintViolationException with the message "Cannot delete or update a parent row: a foreign key constraint fails"

I know it's because of the "many to many" relationship but actually I don't know what to do with this since I am not a MySQL professional. Any help will be appreciated. Thank you.

Edit: here's the database

DROP DATABASE IF EXISTS books;

CREATE DATABASE books;

USE books;

CREATE TABLE Authors
(
   AuthorID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar(30) NOT NULL,
   LastName varchar(30) NOT NULL
)                 ;
CREATE TABLE Titles
(
   ISBN varchar(20) NOT NULL PRIMARY KEY,
   Title varchar(100) NOT NULL,
   EditionNumber int NOT NULL,
   Copyright varchar(4) NOT NULL
)             ;

CREATE TABLE AuthorISBN
(
   AuthorID int NOT NULL,
   ISBN varchar(20) NOT NULL,
   FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID) ON DELETE SET NULL,
   FOREIGN KEY (ISBN) References Titles(ISBN) ON DELETE SET NULL
)            ;

INSERT INTO Authors (FirstName,LastName) VALUES ('Paul','Deitel')           ;
INSERT INTO Authors (FirstName,LastName) VALUES ('Harvey','Deitel')     ;
INSERT INTO Authors (FirstName,LastName) VALUES ('Abbey','Deitel')     ;
INSERT INTO Authors (FirstName,LastName) VALUES ('Michael','Morgano')    ;
INSERT INTO Authors (FirstName,LastName) VALUES ('Eric','Kern')     ;


INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES ('0132152134','Visual Basic 2010 How to Program',5,'2011')          ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (1,'0132152134')     ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (2,'0132152134') ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES ('0132151421','Visual C# 2010 How to Program',4,'2011')  ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (1,'0132151421')      ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (2,'0132151421')      ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES ('0132575663','Java How to Program',9,'2012')   ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (1,'0132575663')                                               ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (2,'0132575663')                                       ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES ('0132662361','C++ How to Program',8,'2012')     ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (1,'0132662361')                                                  ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (2,'0132662361')                                                 ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES ('0132404168','C How to Program',6,'2010')     ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (1,'0132404168')                                             ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (2,'0132404168')                                       ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES ('013705842X','iPhone for Programmers: An App-Driven Approach',1,'2010')        ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (1,'013705842X')                                                    ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (2,'013705842X')                                                    ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (3,'013705842X')                                                    ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (4,'013705842X')                                                    ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (5,'013705842X')                                                    ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES ('0132121360','Android for Programmers: An App-Driven Approach',1,'2012')        ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (1,'0132121360')                                                    ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (2,'0132121360')                                                    ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (3,'0132121360')                                                    ;
INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (4,'0132121360')                                                    ;

回答1:


It is probably because you set your CONSTRAINT onUpdate and onDelete to RESTRICT. You can change it to SET NULL and then be able to delete.

See this section on MySQL documentation.

EDIT

beyond SET NULL, you can try NO ACTION option as well.

IE:

FOREIGN KEY(ord_no,book_id) REFERENCES neworder(ord_no,book_id)  
ON UPDATE SET NULL ON DELETE SET NULL

OR

FOREIGN KEY(ord_no,book_id) REFERENCES neworder(ord_no,book_id)  
ON UPDATE NO ACTION ON DELETE NO ACTION

Hope it helps.




回答2:


Fixed the problem by removing the NOT NULL words and adding ON DELETE CASCADE and ON UPDATE CASCADE after both FOREIGN KEY statements.



来源:https://stackoverflow.com/questions/16497244/delete-from-many-to-many-relationship-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!