SQL Error (1215): Cannot add foreign key constraint
SQL Error (1215): Cannot add foreign key constraint
CREATE TABLE `profilePic` (
`ClientID` VARCHAR(255) NOT NULL,
PRIMARY KEY (`ClientID`),
CONSTRAINT `FK__user_details` FOREIGN KEY (`ClientID`) REFERENCES `user_details` (`ClientID`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
;
我正在尝试使用外键添加 table,但出现此错误,为什么会这样?
- 尝试做新的table。
- 我正在尝试在
user_details->ClientID
和 profilePic->ClientID
上添加相同的详细信息
3.i 已经有一个 table 调用`d userdb 并且在这个 table 中我有 ClientID 和它的外键及其工作。
以下将失败,因为排序规则不同。我为什么要显示这个?因为 OP 没有。
请注意,由于使用该排序规则调整 varchar 255 的大小时出现错误 1071,我缩小了大小,然后自动选择了字符集。
重点是,如果排序规则不同,它将不起作用。
CREATE TABLE `user_details` (
`ClientID` VARCHAR(100) NOT NULL,
PRIMARY KEY (`ClientID`)
)ENGINE=InnoDB;
CREATE TABLE `profilePic` (
`ClientID` VARCHAR(100) NOT NULL,
PRIMARY KEY (`ClientID`),
CONSTRAINT `FK__user_details` FOREIGN KEY (`ClientID`) REFERENCES `user_details` (`ClientID`) ON UPDATE CASCADE ON DELETE CASCADE
)COLLATE='utf8mb4_unicode_ci' ENGINE=InnoDB;
以上故障为table级。可以看到由于 column-level 归类不匹配导致 1215 错误的更棘手的问题 in This answer.
将讨论拉到更一般的情况下...
无论您是尝试在 table 创建时还是使用 ALTER TABLE
建立外键约束
| ADD [CONSTRAINT [symbol]]
FOREIGN KEY [index_name] (index_col_name,...)
reference_definition
比如
ALTER TABLE `facility` ADD CONSTRAINT `fkZipcode`
FOREIGN KEY (`zipcode`) REFERENCES `allzips`(`zipcode`);
以下将适用。
来自标题为 Using FOREIGN KEY Constraints 的 MySQL 手册页:
Corresponding columns in the foreign key and the referenced key must
have similar data types. The size and sign of integer types must be
the same. The length of string types need not be the same. For
nonbinary (character) string columns, the character set and collation
must be the same.
此外,referenced (parent) table 必须有一个 left-most 键可用于快速查找(验证)。 parent 键不需要是 PRIMARY
甚至 UNIQUE
。这个概念在下面的第二块中描述。第一个块暗指 Helper 索引,如果需要,将在 referencing (child) table if太有必要了。
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. Such an
index is created on the referencing table automatically if it does not
exist. This index might be silently dropped later, if you create
another index that can be used to enforce the foreign key constraint.
index_name, if given, is used as described previously.
InnoDB permits a foreign key to reference any 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.
尝试通过 HeidiSQL 创建外键时,只要所选列的数据类型不匹配,您就会收到警告。由于来自服务器 ("Cannot add foreign key constraint")
的非直观消息,我向 HeidiSQL 的 table 设计器添加了此警告
The selected foreign column do not match the source columns data type and unsigned flag. This will give you an error message when trying to save this change. Please compare yourself:
CREATE TABLE `profilePic` (
`ClientID` VARCHAR(255) NOT NULL,
PRIMARY KEY (`ClientID`),
CONSTRAINT `FK__user_details` FOREIGN KEY (`ClientID`) REFERENCES `user_details` (`ClientID`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
;
我正在尝试使用外键添加 table,但出现此错误,为什么会这样?
- 尝试做新的table。
- 我正在尝试在
user_details->ClientID
和profilePic->ClientID
上添加相同的详细信息 3.i 已经有一个 table 调用`d userdb 并且在这个 table 中我有 ClientID 和它的外键及其工作。
以下将失败,因为排序规则不同。我为什么要显示这个?因为 OP 没有。
请注意,由于使用该排序规则调整 varchar 255 的大小时出现错误 1071,我缩小了大小,然后自动选择了字符集。
重点是,如果排序规则不同,它将不起作用。
CREATE TABLE `user_details` (
`ClientID` VARCHAR(100) NOT NULL,
PRIMARY KEY (`ClientID`)
)ENGINE=InnoDB;
CREATE TABLE `profilePic` (
`ClientID` VARCHAR(100) NOT NULL,
PRIMARY KEY (`ClientID`),
CONSTRAINT `FK__user_details` FOREIGN KEY (`ClientID`) REFERENCES `user_details` (`ClientID`) ON UPDATE CASCADE ON DELETE CASCADE
)COLLATE='utf8mb4_unicode_ci' ENGINE=InnoDB;
以上故障为table级。可以看到由于 column-level 归类不匹配导致 1215 错误的更棘手的问题 in This answer.
将讨论拉到更一般的情况下...
无论您是尝试在 table 创建时还是使用 ALTER TABLE
| ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) reference_definition
比如
ALTER TABLE `facility` ADD CONSTRAINT `fkZipcode`
FOREIGN KEY (`zipcode`) REFERENCES `allzips`(`zipcode`);
以下将适用。
来自标题为 Using FOREIGN KEY Constraints 的 MySQL 手册页:
Corresponding columns in the foreign key and the referenced key must have similar data types. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.
此外,referenced (parent) table 必须有一个 left-most 键可用于快速查找(验证)。 parent 键不需要是 PRIMARY
甚至 UNIQUE
。这个概念在下面的第二块中描述。第一个块暗指 Helper 索引,如果需要,将在 referencing (child) table if太有必要了。
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. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.
InnoDB permits a foreign key to reference any 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.
尝试通过 HeidiSQL 创建外键时,只要所选列的数据类型不匹配,您就会收到警告。由于来自服务器 ("Cannot add foreign key constraint")
的非直观消息,我向 HeidiSQL 的 table 设计器添加了此警告The selected foreign column do not match the source columns data type and unsigned flag. This will give you an error message when trying to save this change. Please compare yourself: