简体   繁体   English

MySQL试图删除所有不受外键约束的行

[英]MySQL attempting to delete all rows which are not constrained by foreign key

Okay, this is (probably) a very simple question, but I am afraid I know almost no MySQL, so please put up with me. 好吧,这可能是一个非常简单的问题,但我恐怕几乎不知道MySQL,所以请忍受我。 I'm just trying to delete every row from one table which is not constrained by a Foreign Key in another table - a specific table, there are only two tables involved here. 我只是试图从一个表中删除不受另一个表中的外键约束的每一行 - 一个特定的表,这里只涉及两个表。 The create statements look a bit like: create语句看起来有点像:

CREATE TABLE  `testschema`.`job` (
  `Job_Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Comment` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`Job_Id`) USING BTREE,
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

CREATE TABLE  `ermieimporttest`.`jobassignment` (
  `JobAssignment_Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `JobId` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`JobAssignment_Id`),
  KEY `FK_jobassignment_1` (`JobId`),
  CONSTRAINT `FK_jobassignment_1` FOREIGN KEY (`JobId`) REFERENCES `job` (`Job_Id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Any my SQL statement is: 我的任何SQL语句都是:

DELETE FROM job USING job INNER JOIN jobAssignment WHERE job.Job_Id != jobAssignment.JobId;

I thought this was correct - it should delete every job from the job table for which there does not exist a job assignment which has that job as it's Foreign Key. 我认为这是正确的 - 它应该删除作业表中的每个作业,而作业作业中没有作业,因为它是外键。 However, this fails with the following error when I try and execute it: 但是,当我尝试执行它时失败并出现以下错误:

Cannot delete or update a parent row: a foreign key constraint fails ( testdatabase . jobassignment , CONSTRAINT FK_jobassignment_1 FOREIGN KEY ( JobId ) REFERENCES job ( Job_Id )) 无法删除或更新父行,外键约束失败( testdatabasejobassignment ,约束FK_jobassignment_1外键( JobId )参考jobJob_Id ))

So what silly thing am I doing wrong? 那么我做错了什么傻事呢?

EDIT: As usual, I found an answer only seconds after posting here. 编辑:像往常一样,我在帖子发布后几秒钟就找到了答案。 I used the (completely different) query: 我使用(完全不同的)查询:

DELETE FROM job WHERE Job_Id NOT IN (SELECT JobId FROM jobassignment) 

Out of curiosity, is this the better way to do it? 出于好奇,这是更好的方法吗? Was my original idea even feasible? 我最初的想法是否可行? And if so, what was wrong with it? 如果是这样,它有什么问题?

DELETE FROM job USING job 
LEFT JOIN jobAssignment ON(job.Job_Id = jobAssignment.JobId)
WHERE jobAssignment.JobId IS NULL;

You'll probably need a subquery, not sure if this will work in mySQL, but something similar at least: 您可能需要一个子查询,不确定这是否适用于mySQL,但至少类似于:

DELETE FROM job
WHERE job.Job_Id NOT IN (
  SELECT JobId FROM jobAssignment
)

Naktibalda suggests the subquery may be inefficient; Naktibalda认为子查询可能效率低下; if so you could try 如果是这样你可以试试

DELETE FROM job
     WHERE NOT EXISTS (SELECT *
                           FROM jobassignment
                           WHERE job.Job_Id = jobassignment.Job_Id);

I've had bad experiences with IN and NOT IN in the past; 我过去曾经有过IN和NOT IN的糟糕经历; less trouble with NOT EXISTS. NOT EXISTS减少麻烦。

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

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