从MySQL中删除多对多关系

时间:2013-05-11 12:47:30

标签: mysql many-to-many

我不是MySQL专业人员我只是想创建一个访问MySQL数据库的简单java应用程序(例如着名的书籍/作者数据库)并对其进行操作(即添加,更新,删除)。

问题是,当我尝试使用

删除时,有两个表具有多对多的关系(例如,作者表和书籍表)
DELETE FROM tableName WHERE someColumnName = 'someValue'

它抛出MySQLIntegrityConstraintViolationException,并显示消息“无法删除或更新父行:外键约束失败”

我知道这是因为“多对多”的关系,但实际上我不知道该怎么做,因为我不是MySQL的专业人士。任何帮助将不胜感激。谢谢。

修改 这是数据库

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

2 个答案:

答案 0 :(得分:1)

可能是因为您将CONSTRAINT onUpdateonDelete设置为RESTRICT。您可以将其更改为SET NULL,然后可以删除。

请参阅有关MySQL文档的this section

修改

超越SET NULL,您也可以尝试NO ACTION选项。

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

希望它有所帮助。

答案 1 :(得分:0)

通过移除NOT NULL字词并在 FOREIGN KEY 语句后添加ON DELETE CASCADEON UPDATE CASCADE来解决问题。