简体   繁体   English

如何从与外键连接的表中删除行?

[英]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_SongFileID_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_SongFileID_Img列,并在imagesfilesID_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_nameimage_extensionimage_sizefile_namefile_extensionfile_size看起来不太好。 But there is nothing really wrong with that design.但这种设计并没有什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我必须根据计数从与外键连接的两个表中删除一些行 - I have to delete some rows from two tables which are connected with a foreign key on the basis of counts 如何删除外键为NULL的行? - How can I delete rows where its foreign key is NULL? 如何将通过外键连接的两个表中的所有行迭代到 PHP、CodeIgniter、MySQL 中的单个表中 - How do I iterate all rows from two tables connected by foreign key into a single table in PHP, CodeIgniter, MySQL 如何调试mysql外键约束,ON DELETE CASCADE不能在生产环境中从子表中删除行 - How to debug mysql foreign key constraint, ON DELETE CASCADE not deleting rows from Child table on production environment 用外键从表中删除 - Delete from table with foreign key 如何使用其他表中的外键删除具有主键的行? - How do I delete row with primary key using foreign key from other table? 如何告诉Hibernate通过其外键指向的实体删除表行? - How can I tell Hibernate to delete a table row through the entity it's foreign key points to? 如何在MySQL中从不存在的表中删除不存在的外键? - How do I delete a foreign key that does not exist from a table that does not exist, in MySQL? 如何动态删除MySQL表中的外键? - How to delete a foreign key from MySQL table dynamically? 如何从具有其他条件的表中删除多行? - How can I delete multiple rows from a table with another condition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM