简体   繁体   中英

Self relationship in MySQL

I am trying to add a self relation in an existing Innodb table here is table structure

Table person

   person_id int (10) primary key not null auto increment,

   parent_id int (10) NULL default null,

   name varchar(30)

When I use this command

ALTER TABLE `person` ADD FOREIGN KEY ( `parent_id` ) REFERENCES `person` (`person_id`) ON DELETE RESTRICT ON UPDATE RESTRICT ;

I get the error data type mismatch. I think this could be due to null values in parent_id. Is there any way to skip this check?

Thanks

person_id and parent_id need to be the exact same data type. For example, if person_id is INT UNSIGNED and parent_id is INT, then you can't create the foreign key.

Run this command and compare the data types of the two columns:

SHOW CREATE TABLE `person`\G

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM