无法创建外键约束?
Unable to create foreign key constraint?
我有两个表:Persons 和 person_config 具有以下模式:-
/*Table: persons*/
------------------
/*Column Information*/
----------------------
Field Type Collation Null Key Default Extra Privileges Comment
----------- ----------------- ------------------ ------ ------ ------------------- -------------- ------------------------------- ---------
person_id int(100) unsigned (NULL) NO PRI (NULL) auto_increment select,insert,update,references
person_name varchar(25) utf8mb4_general_ci YES (NULL) select,insert,update,references
added_date date (NULL) YES current_timestamp() select,insert,update,references
added_logon varchar(10) utf8mb4_general_ci YES Admin select,insert,update,references
/*Index Information*/
---------------------
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
------- ---------- -------- ------------ ----------- --------- ----------- -------- ------ ------ ---------- ------- ---------------
persons 0 PRIMARY 1 person_id A 992 (NULL) (NULL) BTREE
和
/*Table: person_config*/
------------------------
/*Column Information*/
----------------------
Field Type Collation Null Key Default Extra Privileges Comment
----------- ----------------- ------------------ ------ ------ ------------------- -------------- ------------------------------- ---------
config_id int(100) unsigned (NULL) NO PRI (NULL) auto_increment select,insert,update,references
person_id int(100) (NULL) NO PRI (NULL) select,insert,update,references
config varchar(25) utf8mb4_general_ci NO (NULL) select,insert,update,references
value varchar(25) utf8mb4_general_ci NO (NULL) select,insert,update,references
added_date timestamp (NULL) NO current_timestamp() select,insert,update,references
added_logon varchar(25) utf8mb4_general_ci YES Admin select,insert,update,references
/*Index Information*/
---------------------
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
------------- ---------- -------- ------------ ----------- --------- ----------- -------- ------ ------ ---------- ------- ---------------
person_config 0 PRIMARY 1 config_id A 1 (NULL) (NULL) BTREE
person_config 0 PRIMARY 2 person_id A 1 (NULL) (NULL) BTREE
我想做一些像 ADD Person_Config(person_id) 指的是人 (person_id)。请找到以下查询:
Alter table `movies`.`person_config`
add foreign key (`person_id`) references `movies`.`persons`(`person_id`)
我收到以下错误:
Can't create table movies
.person_config
(errno: 150 "Foreign key
constraint is incorrectly formed")
请大神指导一下,这有什么问题。
您的 person_config
有 person_id
作为主键。您需要在为该列添加外键关系之前将其删除。
据我所知,您在 person_config
主键上对 person_id
加上 table 上的 person_id 是无符号整数,但在 person_config
外键上是整数父键和子键类型必须完全相同 table 因此删除 person_id
主键约束并使其成为 unsigned int 并重试。
我有两个表:Persons 和 person_config 具有以下模式:-
/*Table: persons*/
------------------
/*Column Information*/
----------------------
Field Type Collation Null Key Default Extra Privileges Comment
----------- ----------------- ------------------ ------ ------ ------------------- -------------- ------------------------------- ---------
person_id int(100) unsigned (NULL) NO PRI (NULL) auto_increment select,insert,update,references
person_name varchar(25) utf8mb4_general_ci YES (NULL) select,insert,update,references
added_date date (NULL) YES current_timestamp() select,insert,update,references
added_logon varchar(10) utf8mb4_general_ci YES Admin select,insert,update,references
/*Index Information*/
---------------------
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
------- ---------- -------- ------------ ----------- --------- ----------- -------- ------ ------ ---------- ------- ---------------
persons 0 PRIMARY 1 person_id A 992 (NULL) (NULL) BTREE
和
/*Table: person_config*/
------------------------
/*Column Information*/
----------------------
Field Type Collation Null Key Default Extra Privileges Comment
----------- ----------------- ------------------ ------ ------ ------------------- -------------- ------------------------------- ---------
config_id int(100) unsigned (NULL) NO PRI (NULL) auto_increment select,insert,update,references
person_id int(100) (NULL) NO PRI (NULL) select,insert,update,references
config varchar(25) utf8mb4_general_ci NO (NULL) select,insert,update,references
value varchar(25) utf8mb4_general_ci NO (NULL) select,insert,update,references
added_date timestamp (NULL) NO current_timestamp() select,insert,update,references
added_logon varchar(25) utf8mb4_general_ci YES Admin select,insert,update,references
/*Index Information*/
---------------------
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
------------- ---------- -------- ------------ ----------- --------- ----------- -------- ------ ------ ---------- ------- ---------------
person_config 0 PRIMARY 1 config_id A 1 (NULL) (NULL) BTREE
person_config 0 PRIMARY 2 person_id A 1 (NULL) (NULL) BTREE
我想做一些像 ADD Person_Config(person_id) 指的是人 (person_id)。请找到以下查询:
Alter table `movies`.`person_config`
add foreign key (`person_id`) references `movies`.`persons`(`person_id`)
我收到以下错误:
Can't create table
movies
.person_config
(errno: 150 "Foreign key constraint is incorrectly formed")
请大神指导一下,这有什么问题。
您的 person_config
有 person_id
作为主键。您需要在为该列添加外键关系之前将其删除。
据我所知,您在 person_config
主键上对 person_id
加上 table 上的 person_id 是无符号整数,但在 person_config
外键上是整数父键和子键类型必须完全相同 table 因此删除 person_id
主键约束并使其成为 unsigned int 并重试。