删除MySQL中的相关记录
我有两个MySQL(MyISAM)表:
Posts: PostID(primary key), post_text, post_date, etc.
Comments: CommentID(primary key), comment_text, comment_date, etc.
当从“Posts”表中删除相应的帖子记录时,我想删除“Comments”表中属于特定帖子的所有评论。
我知道这可以使用 InnoDB 的级联删除来实现(通过设置外键)。 但是我该如何使用 PHP 在 MyISAM 中做到这一点呢?
I have two MySQL (MyISAM) tables:
Posts: PostID(primary key), post_text, post_date, etc.
Comments: CommentID(primary key), comment_text, comment_date, etc.
I want to delete all the comments in the "Comments" table belonging to a particular post, when the corresponding post record is deleted from the "Posts" table.
I know this can be achieved using cascaded delete with InnoDB (by setting up foreign keys). But how would I do it in MyISAM using PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您的 Comments 表有一个字段 PostID,它指定评论所属的帖子。
Assuming your Comments table has a field PostID, which designates the Post to which a Comment belongs to.
即使没有可执行的外键,删除的方法仍然是相同的。 假设您的 Comments 表中有像
post_id
这样的列,那么使用 MyISAM 您真正失去的就是在事务中执行这两个查询的能力。
Even without enforceable foreign keys, the method to do the deletion is still the same. Assuming you have a column like
post_id
in your Comments tableWhat you really lose with MyISAM is the ability to execute these two queries within a transaction.
我从未尝试过,但您可以设置一个触发器来执行级联删除(如果您使用的是 >=5.0)
I've never tried it, but you could set up a trigger to do cascading deletes (if you are using >=5.0)