简体   繁体   English

MySQL:从两个表之一删除行

[英]MySQL: Delete rows from either of two tables

I have two tables setup like this: 我有两个这样的表设置:

user_users

ID    ColA    ColB
55    This    That
56    Other   Stuff

user_meta

ID    UserID    MetaName    MetaValue
1     56        some_name   some_value
2     56        other_name  other_vaue
3     99        this_too    equals_this

So, there can be multiple rows in the user_meta table attributed to a given UserID . 因此, user_meta表中可以有多个行归于给定的UserID When I remove a user, I need to also remove any rows in the user_meta table attributed to that user. 删除用户时,还需要删除归因于该用户的user_meta表中的所有行。

Here's what I have: 这是我所拥有的:

DELETE user_users, user_meta FROM user_users LEFT JOIN user_meta ON user_meta.UserID = user_users.ID WHERE user_users.ID = 56

This will work great in these scenarios: 在以下情况下这将非常有用:

  1. If the user exists in user_users but no rows exist for this user in user_meta 如果该用户存在于user_users但该用户在user_meta不存在任何行
  2. If the user exists in user_users and rows exist for this user in user_meta 如果该用户存在于user_users并且该用户的行存在于user_meta

The problem is that if (for some reason) the user ID exists in user_meta but NOT in user_users , the rows are not removed from the user_meta table. 的问题是,如果(由于某种原因)中存在的用户ID user_meta但不是在user_users ,行不从除去user_meta表。

For all practical reasons, this query should work in all cases (because how would you have user meta saved for a nonexistent user?), but just in case, I'd like to ensure that the query would also remove any rows in the user_meta table with the UserID EVEN IF that user does not exist in the user_users table. 出于所有实际原因,此查询应该在所有情况下都可以工作(因为您将如何为不存在的用户保存用户元数据?),但以防万一,我想确保该查询还可以删除user_meta中的任何行如果用户在user_users表中不存在,则该表具有UserID

So, I'd like this query to remove any rows in the user_meta table with UserID = 99 , but it doesn't, because no ID of 99 exists in the user_users table: 所以,我想这个查询在删除任何行user_meta与表UserID = 99 ,但它没有,因为没有ID的99中存在user_users表:

DELETE user_users, user_meta FROM user_users LEFT JOIN user_meta ON user_meta.UserID = user_users.ID WHERE user_users.ID = 99

How do I update this query to delete from either/both tables regardless if the ID exists on either of the other tables? 我如何更新此查询以从一个/两个表中删除,而不管其他两个表中是否存在该ID?

This situation is a good candidate for using cascading deletion constraints. 这种情况是使用级联删除约束的良好选择。 You want child records in the user_meta table to be deleted whenever a parent record in user_users is deleted. 要在子记录user_meta表每当父记录被删除user_users被删除。 Assuming there is already a foreign key constraint on UserID in the user_meta table, you could try: 假设user_meta表中的UserID已经存在外键约束,则可以尝试:

ALTER TABLE user_meta DROP FOREIGN KEY user_id_key;

ALTER TABLE user_meta
ADD CONSTRAINT user_id_key
FOREIGN KEY (UserID) REFERENCES user_users (ID)
ON DELETE CASCADE;

This assumes that you already have a foreign key constraint on user_meta#UserID . 假设您已经对user_meta#UserID了外键约束。 If you don't, then ignore the first ALTER TABLE statement. 如果不这样做,则忽略第一个ALTER TABLE语句。

With this constraint in place, deleting a user record from user_users would automatically cause all child records in user_meta to be deleted. 在此约束下的地方,从删除用户记录user_users会自动导致所有子记录user_meta被删除。

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

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