mysql删除关系,在MySQL中从多对多关系中删除

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') ;

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值