无法删除外键约束失败

时间:2014-10-15 10:06:47

标签: mysql sql mysql-error-1064

当我尝试从测试中删除问题时,sql显示:

 `Cannot delete or update a parent row: a foreign key constraint fails (`oes`.`studentquestion`,     
 CONSTRAINT `studentquestion_ibfk_2` FOREIGN KEY (`testid`, `qnid`) REFERENCES `question` 
 (`testid`, `qnid`))`

这将是我的表:

学生表

 CREATE TABLE `student` ( 
 `stdid` bigint(20) NOT NULL, 
 `subid` int(11) NOT NULL, 
 `stdname` varchar(40) default NULL, 
 `stdpassword` varchar(40) default NULL, 
 `emailid` varchar(40) default NULL, 
 `contactno` varchar(20) default NULL, 
 `address` varchar(40) default NULL, 
 `city` varchar(40) default NULL, 
 `pincode` varchar(20) default NULL, 
 PRIMARY KEY (`stdid`), 
 UNIQUE KEY `stdname` (`stdname`), 
 UNIQUE KEY `emailid` (`emailid`) 
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

studentquestion表

  CREATE TABLE `studentquestion` ( 
  `stdid` bigint(20) NOT NULL default '0', 
  `testid` bigint(20) NOT NULL default '0', 
  `qnid` int(11) NOT NULL default '0',
 `answered` enum('answered','unanswered','review') DEFAULT NULL,
 `stdanswer` enum('optiona','optionb','optionc','optiond') DEFAULT NULL,
  PRIMARY KEY (`stdid`,`testid`,`qnid`), 
  KEY `testid` (`testid`,`qnid`) 
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

studenttest表

    `stdid` bigint(20) NOT NULL default '0', 
    `testid` bigint(20) NOT NULL default '0', 
    `starttime` timestamp NOT NULL default CURRENT_TIMESTAMP, 
    `endtime` timestamp NOT NULL default '0000-00-00 00:00:00', 
    `correctlyanswered` int(11) default NULL, 
    `status` enum('over','inprogress') default NULL, 
    PRIMARY KEY (`stdid`,`testid`), 
     KEY `testid` (`testid`) 
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

问题表

      CREATE TABLE `question` ( 
      `testid` bigint(20) NOT NULL default '0', 
       `qnid` int(11) NOT NULL default '0', 
       `question` varchar(500) default NULL, 
      `optiona` varchar(100) DEFAULT NULL,
      `optionb` varchar(100) DEFAULT NULL,
      `optionc` varchar(100) DEFAULT NULL,
      `optiond` varchar(100) DEFAULT NULL,
       `correctanswer` enum ('optiona','optionb','optionc','optiond') DEFAULT NULL,
        `marks` int(11) DEFAULT NULL,
       PRIMARY KEY (`testid`,`qnid`) 
      ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

测试表

      CREATE TABLE `test` ( 
     `testid` bigint(20) NOT NULL, 
     `testname` varchar(30) NOT NULL, 
     `testdesc` varchar(100) default NULL, 
     `testdate` date default NULL, 
     `testtime` time default NULL, 
     `subid` int(11) default NULL, 
     `testfrom` timestamp NOT NULL default CURRENT_TIMESTAMP,
     `testto` timestamp NOT NULL default '0000-00-00 00:00:00', 
     `duration` int(11) default NULL, 
     `totalquestions` int(11) default NULL, 
     `attemptedstudents` bigint(20) DEFAULT NULL,
     `testcode` varchar(40) NOT NULL, 
     `tcid` bigint(20) default NULL, 
     `minimunscore` int(11) NOT NULL, 
     PRIMARY KEY (`testid`), 
     UNIQUE KEY `testname` (`testname`), 
     KEY `test_fk1` (`subid`), 
     KEY `test_fk2` (`tcid`) 
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

主题表

     CREATE TABLE `subject` ( 
     `subid` int(11) NOT NULL, 
     `subname` varchar(40) default NULL, 
     `subdesc` varchar(100) default NULL, 
     `tcid` bigint(20) default NULL, 
     PRIMARY KEY (`subid`), 
     UNIQUE KEY `subname` (`subname`), 
     KEY `subject_fk1` (`tcid`) 
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

     ALTER TABLE `studentquestion` 
     ADD CONSTRAINT `studentquestion_ibfk_1` FOREIGN KEY (`stdid`) REFERENCES `student`                              
     (`stdid`),   
     ADD CONSTRAINT `studentquestion_ibfk_2` FOREIGN KEY (`testid`, `qnid`) REFERENCES `question`              
     (`testid`, `qnid`); 
     ALTER TABLE `studenttest`
     ADD CONSTRAINT `studenttest_ibfk_1` FOREIGN KEY (`stdid`) REFERENCES `student` (`stdid`), 
     ADD CONSTRAINT `studenttest_ibfk_2` FOREIGN KEY (`testid`) REFERENCES `test` (`testid`); 
     ALTER TABLE `question` 
     ADD CONSTRAINT `question_ibfk_1` FOREIGN KEY (`testid`) REFERENCES `test` (`testid`); 
     ALTER TABLE `test` 
     ADD CONSTRAINT `test_fk1` FOREIGN KEY (`subid`) REFERENCES `subject` (`subid`), 
     ADD CONSTRAINT `test_fk2` FOREIGN KEY (`tcid`) REFERENCES `testconductor` (`tcid`); 
     ALTER TABLE `subject` 
     ADD CONSTRAINT `subject_fk1` FOREIGN KEY (`tcid`) REFERENCES `testconductor` (`tcid`);

     INSERT INTO `adminlogin` VALUES ('001','root','root');

     INSERT INTO `studenttest` (`stdid`, `testid`, `starttime`, `endtime`, `correctlyanswered`,          
     `status`) VALUES
     (1, 1, '2014-10-15 09:11:24', '2014-10-15 09:21:24', 0, 'over');

     INSERT INTO `subject` (`subid`, `subname`, `subdesc`, `tcid`) VALUES
     (1, 'fref', 'few', NULL);

     INSERT INTO `test` (`testid`, `testname`, `testdesc`, `testdate`, `testtime`, `subid`,          
     `testfrom`, `testto`, `duration`, `totalquestions`, `attemptedstudents`, `testcode`, `tcid`) 
     VALUES

     (1, 'gregre', 'greger', '2014-10-15', '17:08:16', 1, '2014-10-15 03:08:16', '2014-10-16          
     15:59:59', 10, 2, 0, '.ȁ', NULL);

当学生完成测试后,问题将无法删除。无论是测试还是主题。

1 个答案:

答案 0 :(得分:0)

这是因为studentquestion表将留下指向不存在记录的外键。您需要删除具有要删除的记录的外键的行,或者设置CASCADE规则,以便在删除父行时执行操作。

相关问题