当尝试执行将外键分配给我的数据库的SQL语句时,我收到错误,我是否遗漏了一些简单的内容?
CREATE TABLE `Organisation` (
`PK_ORG_ID` INT NOT NULL AUTO_INCREMENT,
`ORG_NAME` VARCHAR(128) NOT NULL,
PRIMARY KEY (`PK_ORG_ID`)
);
CREATE TABLE `User` (
`PK_USER_ID` INT NOT NULL AUTO_INCREMENT,
`USER_NAME` VARCHAR(128) NOT NULL,
`FK_USER_ORG` INT NOT NULL,
PRIMARY KEY (`PK_USER_ID`)
);
CREATE TABLE `Issue` (
`PK_ISSUE_ID` INT NOT NULL AUTO_INCREMENT,
`ISSUE_TEXT` VARCHAR(128) NOT NULL,
`FK_ISSUE_PROPOSED_BY` VARCHAR(255) NOT NULL,
`ISSUE_UPSCORE` INT NOT NULL,
`ISSUE_TYPE` INT NOT NULL,
`ISSUE_TITLE` VARCHAR(255) NOT NULL,
`FK_ISSUE_CANDIDATE` VARCHAR(255),
PRIMARY KEY (`PK_ISSUE_ID`)
);
CREATE TABLE `Vote Card` (
`PK_VOTECARD_ID` INT NOT NULL AUTO_INCREMENT,
`FK_VOTECARD_USERID` INT NOT NULL,
`FK_ISSUE_ID` INT NOT NULL,
`VOTESCORE` INT NOT NULL,
PRIMARY KEY (`PK_VOTECARD_ID`)
);
ALTER TABLE `User` ADD CONSTRAINT `User_fk0` FOREIGN KEY (`FK_USER_ORG`) REFERENCES `Organisation`(`PK_ORG_ID`);
ALTER TABLE `Issue` ADD CONSTRAINT `Issue_fk0` FOREIGN KEY (`FK_ISSUE_PROPOSED_BY`) REFERENCES `User`(`PK_USER_ID`);
ALTER TABLE `Issue` ADD CONSTRAINT `Issue_fk1` FOREIGN KEY (`FK_ISSUE_CANDIDATE`) REFERENCES `User`(`PK_USER_ID`);
ALTER TABLE `Vote Card` ADD CONSTRAINT `Vote Card_fk0` FOREIGN KEY (`FK_VOTECARD_USERID`) REFERENCES `User`(`PK_USER_ID`);
ALTER TABLE `Vote Card` ADD CONSTRAINT `Vote Card_fk1` FOREIGN KEY (`FK_ISSUE_ID`) REFERENCES `Issue`(`PK_ISSUE_ID`);
MySQL生成的错误消息在
之下SQL query:
ALTER TABLE `Issue` ADD CONSTRAINT `Issue_fk0` FOREIGN KEY (`FK_ISSUE_PROPOSED_BY`) REFERENCES `User`(`PK_USER_ID`)
MySQL said: Documentation
1005 - Can't create table `youdecide`.`#sql-142a_de` (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)
感谢您的帮助:)
答案 0 :(得分:3)
PK_USER_ID
为INT
但FK_ISSUE_PROPOSED_BY
为VARCHAR
,外键必须与主键数据类型匹配
答案 1 :(得分:1)
PK_USER_ID
的类型为INT
,其中FK_ISSUE_PROPOSED_BY
类型为VARCHAR