MySQL错误1451

时间:2011-11-07 22:59:55

标签: mysql

命令:

DELETE FROM  `meetings`.`meetingparticipants` WHERE  `meetingparticipants`.`idParticipants` =503 AND  `meetingparticipants`.`idMeetings` =83

我知道我的数据库中有一个简单的错误。我试图从会议参与者表中删除一行并收到以下错误:

#1451 - Cannot delete or update a parent row: a foreign key constraint fails (`meetings`.`invoices`, CONSTRAINT `fk_meetingid` FOREIGN KEY (`fk_meetingid`) REFERENCES `meetingparticipants` (`idMeetings`) ON DELETE NO ACTION ON UPDATE NO ACTION) 

有什么想法吗?我想我需要参与者表和会议参与者表之间的外键关系和更新,但我宁愿确认或看看是否有任何建议?

CREATE TABLE IF NOT EXISTS `invoices` (
  `idinvoices` int(11) NOT NULL AUTO_INCREMENT,
  `fk_invoiceType` int(11) DEFAULT NULL,
  `DocNum` varchar(10) DEFAULT NULL,
  `DZ` varchar(9) DEFAULT NULL,
  `Amount` decimal(10,2) DEFAULT NULL,
  `fk_meetingid` int(11) DEFAULT NULL,
  `fk_participantid` int(11) DEFAULT NULL,
  `invoiceNotes` varchar(255) DEFAULT NULL,
  `invoiceDocuments` blob,
  PRIMARY KEY (`idinvoices`),
  UNIQUE KEY `DOC_Num` (`DocNum`),
  KEY `fk_invoiceType` (`fk_invoiceType`),
  KEY `fk_meetingid` (`fk_meetingid`),
  KEY `fk_participantid` (`fk_participantid`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1014 ;

-- --------------------------------------------------------

--
-- Table structure for table `invoicestatus`
--

CREATE TABLE IF NOT EXISTS `invoicestatus` (
  `idinvoiceStatus` int(11) NOT NULL AUTO_INCREMENT,
  `invoiceStatus` varchar(45) NOT NULL,
  PRIMARY KEY (`idinvoiceStatus`),
  UNIQUE KEY `invoiceStatus_UNIQUE` (`invoiceStatus`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;

-- --------------------------------------------------------

--
-- Table structure for table `invoicestatushistory`
--

CREATE TABLE IF NOT EXISTS `invoicestatushistory` (
  `fk_invoiceStatus` int(11) DEFAULT NULL,
  `statusStamp` datetime DEFAULT NULL,
  `fk_idinvoices` int(11) NOT NULL,
  `idinvoiceStatusHistory` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`idinvoiceStatusHistory`,`fk_idinvoices`),
  KEY `invoiceStatusHistory` (`fk_invoiceStatus`),
  KEY `invoiceid` (`fk_idinvoices`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1015 ;

-- --------------------------------------------------------

--
-- Table structure for table `invoicetypes`
--

CREATE TABLE IF NOT EXISTS `invoicetypes` (
  `idinvoiceTypes` int(11) NOT NULL AUTO_INCREMENT,
  `invoiceType` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`idinvoiceTypes`),
  UNIQUE KEY `invoiceType_UNIQUE` (`invoiceType`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;

-- --------------------------------------------------------

--
-- Table structure for table `meetingparticipants`
--

CREATE TABLE IF NOT EXISTS `meetingparticipants` (
  `idParticipants` int(11) NOT NULL,
  `idMeetings` int(11) NOT NULL,
  `invited` tinyint(1) DEFAULT NULL,
  `attended` tinyint(1) DEFAULT NULL,
  `travelCostsReimbursed` tinyint(1) DEFAULT NULL,
  `ParticipantFeeEligible` tinyint(1) DEFAULT NULL,
  `ParticipantFeeProcessed` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`idParticipants`,`idMeetings`),
  KEY `idParticipants` (`idParticipants`),
  KEY `idMeetings` (`idMeetings`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `meetings`
--

CREATE TABLE IF NOT EXISTS `meetings` (
  `idmeetings` int(11) NOT NULL AUTO_INCREMENT,
  `meetingName` varchar(45) NOT NULL,
  `beginDate` date DEFAULT NULL,
  `endDate` date DEFAULT NULL,
  `location` varchar(45) DEFAULT NULL,
  `meetingNotes` varchar(255) DEFAULT NULL,
  `meetingDocuments` blob,
  `fk_programID` int(110) DEFAULT NULL,
  PRIMARY KEY (`idmeetings`),
  UNIQUE KEY `meetingName_UNIQUE` (`meetingName`),
  KEY `programid` (`fk_programID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=85 ;

-- --------------------------------------------------------

--
-- Table structure for table `participants`
--

CREATE TABLE IF NOT EXISTS `participants` (
  `idparticipants` int(11) NOT NULL AUTO_INCREMENT,
  `firstName` varchar(45) DEFAULT NULL,
  `lastName` varchar(45) NOT NULL,
  `preferredName` varchar(75) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `affiliation` varchar(45) DEFAULT NULL,
  `foreignNational` tinyint(1) DEFAULT NULL,
  `grantPaid` tinyint(1) DEFAULT NULL,
  `bannerid` varchar(9) DEFAULT NULL,
  `participantNotes` varchar(255) DEFAULT NULL,
  `participantDocuments` blob,
  `visaType` int(11) DEFAULT NULL,
  PRIMARY KEY (`idparticipants`),
  UNIQUE KEY `bannerid_UNIQUE` (`bannerid`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=507 ;

-- --------------------------------------------------------

--
-- Table structure for table `programs`
--

CREATE TABLE IF NOT EXISTS `programs` (
  `idprograms` int(11) NOT NULL AUTO_INCREMENT,
  `ProgramName` varchar(45) NOT NULL,
  PRIMARY KEY (`idprograms`),
  UNIQUE KEY `ProgramName_UNIQUE` (`ProgramName`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `userID` int(11) NOT NULL AUTO_INCREMENT,
  `userName` varchar(25) NOT NULL,
  `userPassword` varchar(10) NOT NULL DEFAULT 'password',
  `userLevel` int(11) NOT NULL,
  PRIMARY KEY (`userID`),
  UNIQUE KEY `userName` (`userName`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

-- --------------------------------------------------------

--
-- Table structure for table `visatypes`
--

CREATE TABLE IF NOT EXISTS `visatypes` (
  `idvisaTypes` int(11) NOT NULL AUTO_INCREMENT,
  `visaType` varchar(45) DEFAULT NULL,
  `paymentInstructions` varchar(500) DEFAULT NULL,
  PRIMARY KEY (`idvisaTypes`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `invoices`
--
ALTER TABLE `invoices`
  ADD CONSTRAINT `fk_invoiceType` FOREIGN KEY (`fk_invoiceType`) REFERENCES `invoicetypes` (`idinvoiceTypes`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_meetingid` FOREIGN KEY (`fk_meetingid`) REFERENCES `meetingparticipants` (`idMeetings`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_participantid` FOREIGN KEY (`fk_participantid`) REFERENCES `meetingparticipants` (`idParticipants`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Constraints for table `invoicestatushistory`
--
ALTER TABLE `invoicestatushistory`
  ADD CONSTRAINT `invoiceid` FOREIGN KEY (`fk_idinvoices`) REFERENCES `invoices` (`idinvoices`) ON DELETE NO ACTION ON UPDATE CASCADE,
  ADD CONSTRAINT `invoiceStatusHistory` FOREIGN KEY (`fk_invoiceStatus`) REFERENCES `invoicestatus` (`idinvoiceStatus`) ON DELETE NO ACTION ON UPDATE CASCADE;

--
-- Constraints for table `meetings`
--
ALTER TABLE `meetings`
  ADD CONSTRAINT `programid` FOREIGN KEY (`fk_programID`) REFERENCES `programs` (`idprograms`) ON DELETE NO ACTION ON UPDATE NO ACTION;

2 个答案:

答案 0 :(得分:0)

您在invoices表上有一个名为“fk_meetingid”的外键约束,但引用了meetingparticipants表,这是一个关联表。在我看来,如果发票被链接到会议,它应该直接链接到meetings表。

答案 1 :(得分:0)

从手册:

http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

  

NO ACTION:标准SQL中的关键字。在MySQL中,相当于   限制。 InnoDB拒绝父进程的删除或更新操作   表,如果引用的表中有相关​​的外键值。   有些数据库系统有延迟检查,NO ACTION是   延期检查。在MySQL中,检查外键约束   马上,所以NO ACTION和RESTRICT一样。

如果我了解您的架构(在查看它30秒后不太可能),您可能希望使用ON DELETE CASCADE,以便在删除参与者时删除参与者发票。如果您需要保留发票但删除参与者,请选择ON DELETE SET NULL