2017-06-25 76 views
0

my database structureOneToMany關係。如何刪除行?

你好。我剛剛學會了如何選擇所有相關的數據,比方說,行 tennismatch.ID = 1:

SELECT * FROM tennismatch m 
JOIN tennismatch_tennisset ms 
ON m.`ID` = ms.`TennisMatch_ID` 
JOIN tennisset s 
ON ms.`mapOfSets_ID` = s.`ID` 
JOIN tennisset_game sg 
ON s.`ID` = sg.`TennisSet_ID` 
JOIN game g 
ON sg.`gamesMap_ID` = g.`ID` 
JOIN game_point gp 
ON g.`ID` =gp.`Game_ID` 
JOIN point p 
ON gp.`points_ID` = p.`ID` 
WHERE m.`ID` = 1 

,但我想不出如何刪除這一切。 非常感謝提前。

回答

1

使用DELETE JOIN語句

DELETE m, s, g, p, ms, sg, gp 
FROM tennismatch m 
JOIN tennismatch_tennisset ms ON m.ID = ms.TennisMatch_ID 
JOIN tennisset s ON ms.mapOfSets_ID = s.ID 
JOIN tennisset_game sg ON s.ID = sg.TennisSet_ID 
JOIN game g ON sg.gamesMap_ID = g.ID 
JOIN game_point gp ON g.ID = gp.Game_ID 
JOIN point p ON gp.points_ID = p.ID 
WHERE m.ID = 1 
+0

不能刪除或更新父行,外鍵約束失敗('tennis_scores'.'tennismatch_tennisset',約束'FK_TENNISMATCH_TENNISSET_TennisMatch_ID'外鍵('TennisMatch_ID')參考' tennismatch'('ID')) –

+0

我可以,因爲,禁用外鍵檢查。 –