[英]How can I delete rows from table connected with foreign key?
I have table Songs with columns(ID_Song, Title, ID_Artist, ID_Image, ID_Audio) where ID_Artist, ID_Image, ID_Audio are foreign key.我有带有列(ID_Song、Title、ID_Artist、ID_Image、ID_Audio)的表格歌曲,其中 ID_Artist、ID_Image、ID_Audio 是外键。
Table Artists with columns (ID_Artist, Name) Table Images with columns (ID_Image,filename, extension, size) Table Audios with columns (ID_Audio,filename, extension, size)带有列的表艺术家(ID_Artist,名称)带有列的表图像(ID_Image,文件名,扩展名,大小)带有列(ID_Audio,文件名,扩展名,大小)的表音频
Columns songs.ID_Artist references Artists.ID_Artist, songs.ID_Image references Images.ID_Image and songs.ID_Audio references Audios.ID_Audio, so when I delete a row from table Songs I want the data connected with that row from other tables be deleted as well, but I can't because of the foreign key, is there a way to do it?列songs.ID_Artist 引用Artists.ID_Artist,songs.ID_Image 引用Images.ID_Image 和songs.ID_Audio 引用Audios.ID_Audio,所以当我从表Songs 中删除一行时,我希望与其他表中该行相关的数据也被删除,但我不能因为外键,有没有办法做到这一点?
Table Songs表歌
CREATE TABLE `songs` (
`ID_Song` int(11) NOT NULL AUTO_INCREMENT,
`SongTitle` varchar(100) NOT NULL,
`ID_Artist` int(11) NOT NULL,
`ID_Img` int(11) NOT NULL,
`ID_SongFile` int(11) NOT NULL,
`Approved` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`ID_Song`),
KEY `ID_Artist` (`ID_Artist`),
KEY `ID_SongFile` (`ID_SongFile`),
KEY `ID_Img` (`ID_Img`),
CONSTRAINT `songs_ibfk_1` FOREIGN KEY (`ID_Artist`) REFERENCES `artists` (`ID_Artist`) ON DELETE CASCADE,
CONSTRAINT `songs_ibfk_2` FOREIGN KEY (`ID_SongFile`) REFERENCES `files` (`ID_File`) ON DELETE CASCADE,
CONSTRAINT `songs_ibfk_3` FOREIGN KEY (`ID_Img`) REFERENCES `images` (`ID_Img`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Table Images表格图片
CREATE TABLE `images` (
`ID_Img` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`extension` varchar(10) NOT NULL,
`size` float NOT NULL,
PRIMARY KEY (`ID_Img`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
For the given schema, you need to fetch ID_SongFile
and ID_Img
before you delete a song.对于给定的架构,您需要在删除歌曲之前获取
ID_SongFile
和ID_Img
。 Assuming you want to delete the song with ID=123.假设您要删除 ID=123 的歌曲。 In pure SQL it could be:
在纯 SQL 中,它可能是:
select ID_SongFile, ID_Img into @file_id, @img_id
from songs
where ID_Song = 123;
delete from songs where ID_Song = 123;
delete from images where ID_Img = @img_id;
delete from files where ID_File = @file_id;
Depending on your data logic, it might be better to "reverse" the relation.根据您的数据逻辑,“反转”关系可能会更好。 If files and images are only related to songs, and it's always a one-to-one relation, I would remove the
ID_SongFile
and ID_Img
columns from the songs
table and have a ID_Song
column as foreign key in images
and files
tables.如果文件和图像仅与歌曲相关,并且始终是一对一的关系,我会从
songs
表中删除ID_SongFile
和ID_Img
列,并在images
和files
表ID_Song
列作为外键。 If you define those new FKs with ON DELETE CASCADE
, you would just need to delete the song, and the related file and image will "disappear".如果你用
ON DELETE CASCADE
定义那些新的 FK,你只需要删除这首歌,相关的文件和图像就会“消失”。
You could also just store all image and file information in the songs
table.您也可以将所有图像和文件信息存储在
songs
表中。 But IMHO having the columns image_name
, image_extension
, image_size
, file_name
, file_extension
and file_size
doesn't look very good.但恕我直言,列
image_name
、 image_extension
、 image_size
、 file_name
、 file_extension
和file_size
看起来不太好。 But there is nothing really wrong with that design.但这种设计并没有什么问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.