我试图这样做:
ALTER TABLE `chiang`.`Standings`
ADD CONSTRAINT `Team` FOREIGN KEY (`Team`) REFERENCES `chiang`.`Teams`(`ID`) ON UPDATE CASCADE ON DELETE RESTRICT;
但是我收到了这个错误:
Can't write; duplicate key in table '#sql-6f42_12d414'
我有一些引用team.ID的外键。这是什么导致它?我怎么能绕过它?
DB Schema:
/*Table structure for table `Games` */
DROP TABLE IF EXISTS `Games`;
CREATE TABLE `Games` (
`HomeID` int(11) NOT NULL,
`AwayID` int(11) NOT NULL,
`HomeScore` int(11) NOT NULL,
`AwayScore` int(11) NOT NULL,
`Date` date NOT NULL,
`ResultID` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`ResultID`),
UNIQUE KEY `ResultID_UNIQUE` (`ResultID`),
KEY `HomeTeeam_idx` (`HomeID`),
KEY `AwayTeam_idx` (`AwayID`),
CONSTRAINT `AwayTeam` FOREIGN KEY (`AwayID`) REFERENCES `Teams` (`ID`) ON UPDATE CASCADE,
CONSTRAINT `HomeTeam` FOREIGN KEY (`HomeID`) REFERENCES `Teams` (`ID`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Data for the table `Games` */
/*Table structure for table `PlayerResults` */
DROP TABLE IF EXISTS `PlayerResults`;
CREATE TABLE `PlayerResults` (
`PlayerID` int(11) NOT NULL,
`GameID` int(11) NOT NULL,
`TeamID` int(11) NOT NULL,
`POS` varchar(45) NOT NULL,
`MP` time NOT NULL,
`FG` int(11) NOT NULL,
`FGA` int(11) NOT NULL,
`3P` int(11) NOT NULL,
`3PA` int(11) NOT NULL,
`FT` int(11) NOT NULL,
`FTA` int(11) NOT NULL,
`ORB` int(11) NOT NULL,
`DRB` int(11) NOT NULL,
`TRB` int(11) NOT NULL,
`AST` int(11) NOT NULL,
`STL` int(11) NOT NULL,
`BLK` int(11) NOT NULL,
`TOV` int(11) NOT NULL,
`PF` int(11) NOT NULL,
`PTS` int(11) NOT NULL,
`PlusMinus` int(11) NOT NULL,
PRIMARY KEY (`PlayerID`,`GameID`),
CONSTRAINT `Player` FOREIGN KEY (`PlayerID`) REFERENCES `Players` (`ID`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Data for the table `PlayerResults` */
/*Table structure for table `Players` */
DROP TABLE IF EXISTS `Players`;
CREATE TABLE `Players` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`FirstName` varchar(45) NOT NULL,
`LastName` varchar(45) NOT NULL,
`Team` int(11) NOT NULL,
PRIMARY KEY (`ID`,`FirstName`,`LastName`,`Team`),
KEY `Team_idx` (`Team`),
CONSTRAINT `Team` FOREIGN KEY (`Team`) REFERENCES `Teams` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Data for the table `Players` */
/*Table structure for table `Standings` */
DROP TABLE IF EXISTS `Standings`;
CREATE TABLE `Standings` (
`Team` int(11) NOT NULL,
`Wins` int(11) NOT NULL DEFAULT '0',
`Losses` int(11) NOT NULL DEFAULT '0',
`Year` year(4) NOT NULL,
PRIMARY KEY (`Team`,`Year`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Data for the table `Standings` */
/*Table structure for table `Teams` */
DROP TABLE IF EXISTS `Teams`;
CREATE TABLE `Teams` (
`ID` int(11) NOT NULL,
`Name` varchar(45) NOT NULL,
`City` varchar(45) NOT NULL,
`Acronym` varchar(45) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `ID_UNIQUE` (`ID`),
UNIQUE KEY `Name_UNIQUE` (`Name`),
UNIQUE KEY `City_UNIQUE` (`City`),
UNIQUE KEY `Acronym_UNIQUE` (`Acronym`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Data for the table `Teams` */
答案 0 :(得分:1)
如果给出了CONSTRAINT符号子句,则符号值在数据库中必须是唯一的。如果没有给出该子句,InnoDB会自动创建名称。
请注意,从版本5.0.38开始,InnoDB允许列上有两个或更多外键,它们可能引用不同的表/列。它甚至允许具有相同定义但具有不同约束名称的外键。
Constraint
名称必须是跨数据库的唯一。
ALTER TABLE `chiang`.`Standings`
ADD CONSTRAINT `Team`
FOREIGN KEY (`Team`)
REFERENCES `chiang`.`Teams`(`ID`)
ON UPDATE CASCADE
ON DELETE RESTRICT;
失败。因为,您已经在表Team
中定义了名为Players
的约束。
CREATE TABLE `Players` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`FirstName` varchar(45) NOT NULL,
`LastName` varchar(45) NOT NULL,
`Team` int(11) NOT NULL,
PRIMARY KEY (`ID`,`FirstName`,`LastName`,`Team`),
KEY `Team_idx` (`Team`),
CONSTRAINT `Team`
FOREIGN KEY (`Team`)
REFERENCES `Teams` (`ID`)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
确保为在数据库中的任何表上定义的每个约束定义UNIQUE。
答案 1 :(得分:-1)
SET FOREIGN_KEY_CHECKS = 0;
[your query here]
SET FOREIGN_KEY_CHECKS = 1;