删除在其他表中没有引用的行;

时间:2013-01-02 17:15:39

标签: mysql sql reference

我想删除数据库中的所有行,这些行在另一个表中没有引用:

这没关系:

mysql> DELETE FROM auth_user WHERE username = 'Sample41';
Query OK, 1 row affected (0.02 sec)

这不行:

mysql> DELETE FROM auth_user WHERE username = 'Sample98';
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`mydatabase`.`school_subjects`, CONSTRAINT `user_id_refs_id_9e8f6fd7` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))

所以我的目标是获取一个查询,删除所有用户(他们将以'Sample'开头),这些用户在其他表中没有引用(实际上只有一个表'school_subjects')

谢谢。,

更新:

mysql> describe school_subjects;
+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| id               | int(11)      | NO   | PRI | NULL    | auto_increment |
| name             | varchar(500) | NO   |     | NULL    |                |
| user_id          | int(11)      | NO   | MUL | NULL    |                |
| created_by       | varchar(64)  | NO   |     | NULL    |                |
| created_time     | datetime     | NO   |     | NULL    |                |
| num_of_followers | int(11)      | NO   |     | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> describe auth_user;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| username     | varchar(30)  | NO   | UNI | NULL    |                |
| first_name   | varchar(30)  | NO   |     | NULL    |                |
| last_name    | varchar(30)  | NO   |     | NULL    |                |
| email        | varchar(75)  | NO   |     | NULL    |                |
| password     | varchar(128) | NO   |     | NULL    |                |
| is_staff     | tinyint(1)   | NO   |     | NULL    |                |
| is_active    | tinyint(1)   | NO   |     | NULL    |                |
| is_superuser | tinyint(1)   | NO   |     | NULL    |                |
| last_login   | datetime     | NO   |     | NULL    |                |
| date_joined  | datetime     | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)

3 个答案:

答案 0 :(得分:5)

  

所以我的目标是获得一个将删除所有用户的查询(他们会   以'Sample'开头,在其他表中没有引用(   实际上只有一个表'school_subjects')

你有一个自引用表。如果您要删除所有记录,则必须包含school_subjectsauth_user。尝试:

DELETE FROM auth_user WHERE username not in (SELECT id FROM auth_user) 
and  username not in (SELECT user_id FROM school_subjects);

更新:如果您没有自我引用约束并且只关心'school_subjects',请尝试以下查询,注意它将删除与约束不冲突的所有记录:

DELETE FROM auth_user WHERE username not in (SELECT user_id FROM school_subjects);

如果您需要删除一些,则需要:

DELETE FROM auth_user 
WHERE username in ('SomeRecordToBeDeleted', 'one more') AND
username not in (SELECT user_id FROM school_subjects);

答案 1 :(得分:1)

这将删除'school_subjects'表中没有匹配'user_id'的行。

DELETE FROM auth_user 
WHERE id NOT IN (SELECT user_id FROM school_subjects);

答案 2 :(得分:1)

你可以试试这个 -

Delete from auth_user where username like 'Sample%' ANd auth_user.id NOT IN 
( select user_id from school_subjects where auth_user.id = school_subjects.user_id)