我有4张桌子:
如果面试官没有在特定的研究中使用,我希望能够删除研究/访谈员关系。我不想从interviewerLkup表中删除面试官,只需从studyInterviewers表中删除。
我遇到的问题是,如果在参与者表格中使用了面试官(对于另一项研究),它不会让我删除学习/面试官关系。
请注意,我要删除特定的元组。我不想删除参与者表中未使用的所有学习/访调员。我正在使用删除过程删除未正确分配到特定研究的特定访调员。
有人可以解释一下如何做到这一点吗?
我使用失败的php / mySql代码是:
$sql1 = "DELETE FROM studyinterviewers
WHERE (StudyID = '".$StudyID."')
AND (InterviewerID = '".$InterviewerID."');";
下面给出了这个迷你架构的SQL代码。我是否正确设置了参与者表中的外键?
CREATE TABLE IF NOT EXISTS `interviewcodes`.`studylkup` (
`StudyID` INT(11) NOT NULL AUTO_INCREMENT,
`StudyName` VARCHAR(45) NOT NULL,
PRIMARY KEY (`StudyID`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `interviewcodes`.`interviewerlkup` (
`InterviewerID` INT(11) NOT NULL AUTO_INCREMENT,
`InterviewerFirstName` VARCHAR(45) NOT NULL,
`InterviewerLastName` VARCHAR(45) NOT NULL,
PRIMARY KEY (`InterviewerID`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `interviewcodes`.`studyinterviewers` (
`StudyID` INT(11) NOT NULL,
`InterviewerID` INT(11) NOT NULL,
PRIMARY KEY (`StudyID`, `InterviewerID`),
INDEX `fk_StudyInterviewers_InterviewerLkup1_idx` (`InterviewerID` ASC),
CONSTRAINT `fk_StudyInterviewers_InterviewerLkup1`
FOREIGN KEY (`InterviewerID`)
REFERENCES `interviewcodes`.`interviewerlkup` (`InterviewerID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_StudyInterviewers_StudyLkup1`
FOREIGN KEY (`StudyID`)
REFERENCES `interviewcodes`.`studylkup` (`StudyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `interviewcodes`.`participant` (
`ParticipantID` INT(11) NOT NULL AUTO_INCREMENT,
`ParticipantCaseID` VARCHAR(45) NOT NULL,
`StudyID` INT(11) NOT NULL,
`InterviewerID` INT(11) NOT NULL,
PRIMARY KEY (`ParticipantID`),
INDEX `fk_participant_studyinterviewers1_idx` (`InterviewerID` ASC),
CONSTRAINT `fk_participant_studyinterviewers1`
FOREIGN KEY (`InterviewerID`)
REFERENCES `interviewcodes`.`studyinterviewers` (`InterviewerID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_participant_studylkup1`
FOREIGN KEY (`StudyID`)
REFERENCES `interviewcodes`.`studylkup` (`StudyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
答案 0 :(得分:0)
您可以使用内部联接从多个表中删除。