我有两个表:具有以下架构的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(person_id)。请找到以下查询:
Alter table `movies`.`person_config`
add foreign key (`person_id`) references `movies`.`persons`(`person_id`)
出现以下错误:
无法创建表
movies
。person_config
(错误号:150“外键 约束的格式不正确”)
亲爱的指导我,这是怎么回事。
答案 0 :(得分:1)
您的person_config
以person_id
作为主键。您需要先删除它,然后才能为该列添加外键关系。
答案 1 :(得分:1)
根据我的见解,您对person_id
主键上的person_config
加上人员表上的person_id是unsigned int,但对person_config
外键类型的int必须与父级上的完全相同,并且子表,因此请删除person_id
主键约束,并将其设置为unsigned int,然后重试。