无法在数据库中创建外键
Unable to create foreign key in database
我正在尝试创建一个 table 并将 varchar 列作为外键,但是 MySQL 在创建 table 时给我一个错误。我的查询是这样的:
CREATE TABLE `survey_hesco_subdivision` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`circle_code` VARCHAR(100) DEFAULT NULL,
`name` VARCHAR(100) DEFAULT NULL,
`circle_name` VARCHAR(100) DEFAULT NULL,
`division_code` VARCHAR(100) DEFAULT NULL,
`sub_div_code` VARCHAR(100) NOT NULL,
`division_name` VARCHAR(100) DEFAULT NULL,
PRIMARY KEY (`id`,`sub_div_code`),
KEY `id` (`id`)
) ENGINE=INNODB AUTO_INCREMENT=91 DEFAULT CHARSET=latin1;
以上table已被使用
Create table `accurate_mam`.`meter_ping`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`meter_id` int(11) NOT NULL,
`meter_msn` varchar(100),
`sub_div_code` varchar(100) NOT NULL,
`sub_div_name` varchar(100),
primary key (`id`),
constraint `FK_PING_METER_ID` foreign key (`meter_id`) references
`accurate_mam`.`meters`(`id`) on delete Cascade,
constraint `FK_PIN_SUB_DIV` foreign key (`sub_div_code`) references
`accurate_mam`.`survey_hesco_subdivision`(`sub_div_code`) on delete Cascade
) ENGINE=InnoDB charset=latin1 collate=latin1_swedish_ci
我收到的错误是
Error Number : 1005
Error Message: Can't create table accurate_mam
.meter_ping
(errno: 150 "Foreign key constraint is incorrectly formed")
我已经研究过这个question
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order.
InnoDB permits a foreign key to reference any index column or group of
columns. However, in the referenced table, there must be an index
where the referenced columns are listed as the first columns in the
same order.
因此,在创建子 table 之前创建一个这样的索引:
CREATE INDEX `idx_survey_hesco_subdivision_sub_div_code` ON survey_hesco_subdivision(sub_div_code);
尽管如此,使用非唯一列作为关系中的参考列并不是最佳做法。 DELETE CASCADE
在这种情况下将无法正常运行。我建议您在主 table 的 sub_div_code
上也创建一个唯一密钥。
详情请参考this
来源:Cannot add foreign key - Whosebug
你已经运行CREATE TABLE meters
了吗?错误是由于缺少 table 引起的。让我们看看 CREATE
.
我正在尝试创建一个 table 并将 varchar 列作为外键,但是 MySQL 在创建 table 时给我一个错误。我的查询是这样的:
CREATE TABLE `survey_hesco_subdivision` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`circle_code` VARCHAR(100) DEFAULT NULL,
`name` VARCHAR(100) DEFAULT NULL,
`circle_name` VARCHAR(100) DEFAULT NULL,
`division_code` VARCHAR(100) DEFAULT NULL,
`sub_div_code` VARCHAR(100) NOT NULL,
`division_name` VARCHAR(100) DEFAULT NULL,
PRIMARY KEY (`id`,`sub_div_code`),
KEY `id` (`id`)
) ENGINE=INNODB AUTO_INCREMENT=91 DEFAULT CHARSET=latin1;
以上table已被使用
Create table `accurate_mam`.`meter_ping`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`meter_id` int(11) NOT NULL,
`meter_msn` varchar(100),
`sub_div_code` varchar(100) NOT NULL,
`sub_div_name` varchar(100),
primary key (`id`),
constraint `FK_PING_METER_ID` foreign key (`meter_id`) references
`accurate_mam`.`meters`(`id`) on delete Cascade,
constraint `FK_PIN_SUB_DIV` foreign key (`sub_div_code`) references
`accurate_mam`.`survey_hesco_subdivision`(`sub_div_code`) on delete Cascade
) ENGINE=InnoDB charset=latin1 collate=latin1_swedish_ci
我收到的错误是
Error Number : 1005 Error Message: Can't create table
accurate_mam
.meter_ping
(errno: 150 "Foreign key constraint is incorrectly formed")
我已经研究过这个question
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order.
InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.
因此,在创建子 table 之前创建一个这样的索引:
CREATE INDEX `idx_survey_hesco_subdivision_sub_div_code` ON survey_hesco_subdivision(sub_div_code);
尽管如此,使用非唯一列作为关系中的参考列并不是最佳做法。 DELETE CASCADE
在这种情况下将无法正常运行。我建议您在主 table 的 sub_div_code
上也创建一个唯一密钥。
详情请参考this
来源:Cannot add foreign key - Whosebug
你已经运行CREATE TABLE meters
了吗?错误是由于缺少 table 引起的。让我们看看 CREATE
.