为什么这个外键失败
why does this foreign key fail
CREATE TABLE `fitness_pledges` (
`ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(250) NOT NULL,
`last_name` VARCHAR(250) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`token` BIGINT(20) UNSIGNED NOT NULL,
`email_valid` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`entered_sweeps` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`referrals` INT(10) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
UNIQUE INDEX `email` (`email`),
UNIQUE INDEX `token` (`token`)
)
COMMENT='holds validation token, first name, last name, email address, number of referrals and whether sweepstakes was entered\r\n'
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
CREATE TABLE `finess_referrals` (
`ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(250) NOT NULL,
`last_name` VARCHAR(250) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`token` BIGINT(20) UNSIGNED NOT NULL,
`referral_token` BIGINT(20) UNSIGNED NOT NULL,
`referral_validated` TINYINT(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
INDEX `token_FK` (`token`),
INDEX `referral_token_FK` (`referral_token`),
CONSTRAINT `referral_token_FK` FOREIGN KEY (`referral_token`) REFERENCES `fitness_pledges` (`token`) ON DELETE NO ACTION,
CONSTRAINT `token_FK` FOREIGN KEY (`token`) REFERENCES `fitness_pledges` (`token`) ON DELETE NO ACTION
)
COMMENT='holds the first name, last name, email and validation token of the referrer as well as the token of the referred individual and a flag for whether the referred individual\'s email has been validated'
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
此更改 Table,由 HeidiSQL 生成,尝试为组合的 first_name、last_name 和电子邮件字段添加外键失败,外键约束形成不正确。字段都定义相同,排序规则相同。为什么会失败?
ALTER TABLE `finess_referrals`
ADD CONSTRAINT `first_last_email_FK` FOREIGN KEY (`first_name`, `last_name`, `email`)
REFERENCES `fitness_pledges` (`first_name`, `last_name`, `email`) ON DELETE NO ACTION;
您首先需要在 fitness_pledges
table 中为 (first_name, last_name, email)
建立索引。来自 Using FOREIGN KEY Constraints:
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan.
它会在必要时自动在引用 table 中创建一个索引,但它不会为引用的 table.
这样做
所以你需要做:
CREATE INDEX first_last_email ON fitness_pledges (first_name, last_name, email);
根据 SQL 标准,外键必须引用父键 table 的主键或唯一键。如果主键有多列,外键必须有相同的列数和列序
CREATE TABLE `fitness_pledges` (
`ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(250) NOT NULL,
`last_name` VARCHAR(250) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`token` BIGINT(20) UNSIGNED NOT NULL,
`email_valid` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`entered_sweeps` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`referrals` INT(10) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
UNIQUE INDEX `email` (`email`),
UNIQUE INDEX `token` (`token`)
)
COMMENT='holds validation token, first name, last name, email address, number of referrals and whether sweepstakes was entered\r\n'
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
CREATE TABLE `finess_referrals` (
`ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(250) NOT NULL,
`last_name` VARCHAR(250) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`token` BIGINT(20) UNSIGNED NOT NULL,
`referral_token` BIGINT(20) UNSIGNED NOT NULL,
`referral_validated` TINYINT(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
INDEX `token_FK` (`token`),
INDEX `referral_token_FK` (`referral_token`),
CONSTRAINT `referral_token_FK` FOREIGN KEY (`referral_token`) REFERENCES `fitness_pledges` (`token`) ON DELETE NO ACTION,
CONSTRAINT `token_FK` FOREIGN KEY (`token`) REFERENCES `fitness_pledges` (`token`) ON DELETE NO ACTION
)
COMMENT='holds the first name, last name, email and validation token of the referrer as well as the token of the referred individual and a flag for whether the referred individual\'s email has been validated'
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
此更改 Table,由 HeidiSQL 生成,尝试为组合的 first_name、last_name 和电子邮件字段添加外键失败,外键约束形成不正确。字段都定义相同,排序规则相同。为什么会失败?
ALTER TABLE `finess_referrals`
ADD CONSTRAINT `first_last_email_FK` FOREIGN KEY (`first_name`, `last_name`, `email`)
REFERENCES `fitness_pledges` (`first_name`, `last_name`, `email`) ON DELETE NO ACTION;
您首先需要在 fitness_pledges
table 中为 (first_name, last_name, email)
建立索引。来自 Using FOREIGN KEY Constraints:
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan.
它会在必要时自动在引用 table 中创建一个索引,但它不会为引用的 table.
这样做所以你需要做:
CREATE INDEX first_last_email ON fitness_pledges (first_name, last_name, email);
根据 SQL 标准,外键必须引用父键 table 的主键或唯一键。如果主键有多列,外键必须有相同的列数和列序