简体   繁体   English

多次删除,一张表为空时出现未知表错误

[英]Multi delete, unknown table error when one table is empty

I delete from two tables with primary entry.我从两个带有主要条目的表中删除。

  • Both tables use to store image id.两个表都用于存储图像 ID。

  • One for display image and other is to save in favourite.一种用于显示图像,另一种用于收藏。

  • When both tables has entry, multi delete query works fine.当两个表都有条目时,多删除查询工作正常。

But:但:

  • When favourite table is empty, query doesn't work for one table and show error as favourite is unknown table.当最喜欢的表为空时,查询不适用于一张表并显示错误,因为最喜欢的表是未知的。

  • Do you have any experiences multi delete from two tables (when one is empty)?您是否有从两个表中多次删除的经验(当一个表为空时)?

I tried in various ways but same errors:我尝试了各种方法,但错误相同:

$sql = "DELETE FROM stat, s_images, favourites USING stat, s_images,
        favourites WHERE favourites.image=stat.id AND stat.id=s_images.id AND
        stat.id=" . $this->id;

$sql = "DELETE stat, s_images,favourites FROM stat, s_images,
        favourites WHERE favourites.image=stat.id AND stat.id=s_images.id AND
        stat.id=" . $this->id;

Try using an explicit inner join尝试使用显式内部联接

DELETE stat, s_images, favourites
FROM stat
INNER JOIN s_images
ON s_images.id = stat.id
INNER JOIN favourites
ON favourites.image = stat.id
WHERE stat.id = $this->id;

This will cause nothing to be deleted when a match is not found in any of the joins.当在任何连接中找不到匹配项时,这将导致不会删除任何内容。

Example: http://sqlfiddle.com/#!9/580be/2示例: http : //sqlfiddle.com/#!9/580be/2

Otherwise you may need to perform individual queries or utilize Foreign Key Constraints to Cascade on delete.否则,您可能需要执行单独的查询或在删除时使用外键约束来级联。

You could also ignore the errors, but this is bad practice.您也可以忽略错误,但这是不好的做法。

DELETE IGNORE stat, s_images, favourites
FROM stat
LEFT JOIN s_images
ON s_images.id = stat.id
LEFT JOIN favourites
ON favourites.image = stat.id
WHERE stat.id = $this->id;

UPDATE with Foreign Keys使用外键更新

Add constraint for s_images.id + stat.ids_images.id + stat.id添加约束

ALTER TABLE `stat`
    ADD CONSTRAINT `FK_stat_s_images` FOREIGN KEY (`id`) REFERENCES `s_images` (`id`) ON UPDATE CASCADE ON DELETE CASCADE;

Add constraint for s_images.id + favourites.images_images.id + favourites.image添加约束

ALTER TABLE `favourites`
    ADD CONSTRAINT `FK_favourites_s_images` FOREIGN KEY (`image`) REFERENCES `s_images` (`id`) ON UPDATE CASCADE ON DELETE CASCADE;

Then all you need to do is execute the query to delete the images, MySQL will ensure your records are managed appropriately, and will aid in optimizations.然后您需要做的就是执行查询以删除图像,MySQL 将确保您的记录得到适当的管理,并有助于优化。

$sql = "DELETE FROM s_images
WHERE s_images.id = '" . $this->id . "'";

Special Considerations特别注意事项

  1. Tables must use the InnoDB engine as foreign keys do not work with MyISAM.表必须使用 InnoDB 引擎,因为外键不适用于 MyISAM。
ALTER TABLE `stat` ENGINE=InnoDB;
ALTER TABLE `favourites` ENGINE=InnoDB;
ALTER TABLE `s_images` ENGINE=InnoDB;
  1. INSERT and UPDATE must use proper ordering, meaning you can not create an association on favourites.image = 1 when the record for s_images.id = 1 does not exist. INSERT 和 UPDATE 必须使用正确的顺序,这意味着当s_images.id = 1的记录不存在时,您不能在favourites.image = 1上创建关联。 The record s_images.id = 1 would need to be created prior to the favourites record.记录s_images.id = 1需要在favourites记录之前创建。

  2. You will not be able to add the FOREIGN KEY constraints when an id is referenced that does not exist.当引用不存在的 id 时,您将无法添加 FOREIGN KEY 约束。 You can override this behavior by using您可以通过使用覆盖此行为

SET FOREIGN_KEY_CHECKS=0;
 ALTER TABLE `stat`
        ADD CONSTRAINT `FK_stat_s_images` FOREIGN KEY (`id`) REFERENCES `s_images` (`id`) ON UPDATE CASCADE ON DELETE CASCADE;
SET FOREIGN_KEY_CHECKS=1;
  1. In order to prevent deletion of records, for historical reference purposes, you can set the constraint to set NULL instead of cascade.为了防止删除记录,为了历史参考目的,可以设置约束设置为NULL而不是级联。
ALTER TABLE `favourites`
    ADD CONSTRAINT `FK_favourites_s_images` FOREIGN KEY (`image`) REFERENCES `s_images` (`id`) ON UPDATE CASCADE ON DELETE SET NULL;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM