无法添加或更新子行:外键约束失败(俱乐部和用户)

时间:2014-04-12 10:56:44

标签: sql database phpmyadmin

用户表

CREATE TABLE IF NOT EXISTS `users` (
  `userId` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `firstname` varchar(25) NOT NULL,
  `lastname` varchar(25) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(32) NOT NULL,
  `addressLine1` varchar(80) NOT NULL,
  `addressLine2` varchar(80) NOT NULL,
  `town` varchar(30) NOT NULL,
  `county` varchar(30) NOT NULL,
  PRIMARY KEY (`userId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=55 ;

俱乐部表

CREATE TABLE IF NOT EXISTS `clubs` (
  `clubId` int(11) NOT NULL AUTO_INCREMENT,
  `clubName` varchar(100) NOT NULL,
  `startTime` varchar(5) NOT NULL,
  `finishTime` varchar(5) NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY (`clubId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

memberid表

   CREATE TABLE IF NOT EXISTS `membersid` (
  `memberId` int(11) NOT NULL AUTO_INCREMENT,
  `clubId` int(11) NOT NULL,
  `userId` int(11) NOT NULL,
  PRIMARY KEY (`memberId`,`clubId`,`userId`),
  UNIQUE KEY `memberId` (`memberId`),
  KEY `clubId` (`clubId`),
  KEY `userId` (`userId`),
  KEY `clubId_2` (`clubId`),
  KEY `clubId_3` (`clubId`),
  KEY `userId_2` (`userId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

DAO

String query = "INSERT INTO membersid( userId, clubId ) VALUES ( ?,? )";

继续获取错误无法添加或更新子行:外键约束失败 如果有人可能那会很棒:)

1 个答案:

答案 0 :(得分:0)

membersid表中的密钥(请将该名称更改为members)搞砸了。尝试

CREATE TABLE IF NOT EXISTS members 
(
  `memberId` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `clubId` int(11) NOT NULL,
  `userId` int(11) NOT NULL,
   UNIQUE KEY (`clubId`,`userId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;