无法创建外键约束?

问题描述 投票:0回答:2

我有两个表:具有以下架构的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`)

出现以下错误:

无法创建表moviesperson_config(错误号:150“外键约束的格式不正确“)

亲切的指导我,这是怎么了。

mysql foreign-keys mariadb primary-key
2个回答
1
投票

根据我的观察,您已经使person_id主键上的person_config加上人员表上的person_id为unsigned int,但person_config外键类型上的int在父表和子表上必须完全相同,因此删除person_id主键约束,并将其设置为unsigned int,然后重试。


1
投票

您的person_config具有person_id作为主键。您需要先删除它,然后才能为该列添加外键关系。

© www.soinside.com 2019 - 2024. All rights reserved.