查了之前的帖子,没找到答案,就这样吧……
我有这张桌子
CREATE TABLE `leags`
(
`lid` int(10) NOT NULL AUTO_INCREMENT,
`lName` varchar(255) NOT NULL,
`description` varchar(255) DEFAULT NULL,
`links` varchar(255) DEFAULT NULL,
`cid` int(10) NOT NULL,
PRIMARY KEY (`lid`),
KEY `index2` (`lid`,`lName`),
KEY `index3` (`lid`,`lName`,`cid`),
KEY `cid_idx` (`cid`),
CONSTRAINT `cid` FOREIGN KEY (`cid`) REFERENCES `cats` (`cid`)
)
ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='Leagues'$$
我有另一个表,上面的 PK 作为外键。
CREATE TABLE `tams`
(
`tId` INT(10) NOT NULL ,
`tName` VARCHAR(255) NOT NULL ,
`lid` INT(10) NULL ,
`url` TEXT NULL ,
PRIMARY KEY (`tId`) ,
INDEX `index2` (`tId` ASC, `tName` ASC) ,
INDEX `index3` (`tId` ASC, `tName` ASC, `lid` ASC) ,
INDEX `lid_idx` (`lid` ASC) ,
CONSTRAINT `lid`
FOREIGN KEY (`lid` )
REFERENCES leags` (`lid` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
该表的创建工作正常,但我在创建下表时遇到了问题。
CREATE TABLE IF NOT EXISTS spSched
(
`spscId` INT(10) NOT NULL AUTO_INCREMENT ,
`startDate` DATE NOT NULL ,
`endDate` DATE NULL ,
`startTime` TIME NOT NULL ,
`endTime` TIME NULL ,
`htId` INT(10) NULL ,
`type` VARCHAR(45) NULL ,
`atId` INT(10) NULL ,
`leagId` INT(10) NULL ,
PRIMARY KEY (`spscId`) ,
UNIQUE INDEX `spscId_UNIQUE` (`spscId` ASC) ,
INDEX `htId_idx` (`htId` ASC) ,
INDEX `atId_idx` (`atId` ASC) ,
INDEX `lid_idx` (`leagId` ASC) ,
CONSTRAINT `tId`
FOREIGN KEY (`htId` )
REFERENCES tams (`tId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `atId`
FOREIGN KEY (`atId` )
REFERENCES tams` (`tId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `lId`
FOREIGN KEY (`leagId` )
REFERENCES leags (`lid` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
如果我从上面的 sql 中删除作为 FK 的 lId 和约束,则表创建得很好。我无法弄清楚重复键在哪里。非常感谢所有帮助。谢谢。