Mysql reports errno150, Can't create table error when creating foreign keys

Error reason 1: The character types of the primary key and foreign key do not match

First create two tables as follows and then execute it. It is found that the creation of the two table fails. After checking, the reason is analyzed because the character type of the primary key and the foreign key do not match. The id of the one table is of type int, and the tid of table two is char(10 ) Type, then as long as it is changed to a consistent character type, the problem will be solved.
If you find that the table you created has the same primary key and foreign key character type, then consider other possibilities, please continue.

create table one(
    id int primary key,
    oname VARCHAR(30)
);
create table two(
    tid char(10),
    tname VARCHAR(30),
    constraint t_o_sk foreign key (tid) references one(id) on delete cascade on update cascade  
##on delete cascade是级联删除,on update cascade 是级联更新
);

Error reason 2: The primary key of the primary table is not the primary key.
If it is, and the table is created again, you can use alter to modify it.
If this problem is found to be unsolved, then the third case may be considered below.

alter table one modify id  int primary key;

Error reason 3: Check whether the character encoding is consistent
Mysql reports errno150, Can't create table error when creating foreign keys

Error reason 4: The column referenced by the foreign key cannot be found. When the foreign key is
added, there may already be data (historical data, or the default value set when the database table is updated), but these data may not necessarily be associated with the foreign key Find the corresponding row in the table. This situation will also cause the foreign key creation to fail.

Guess you like

Origin blog.51cto.com/000011211684/2569663