MyISAM and InnoDB foreign key conflicts during MySQL table Creation

Source: Internet
Author: User

In MySQL, a table of the InnoDB type uses a foreign key to reference the primary key in the MyISAM table. An error occurs during table creation!

 

For example, create a user table users

 

Create Table users

(

Id int not null primary key auto_increment,

Username varchar (32) not null,

Password varchar (32)

) Engine = MyISAM;

 

Then create the user information table userinfo. The foreign key references the user table users.

 

Create Table userinfo

(

Id int not null primary key auto_increment,

User_id int not null,

......,

Constraint fk_user_id foreign key (user_id) References users (ID)

) Engine = InnoDB;

 

The system reports the following error:

Error 1005 (hy000): Can't create table 'test. userinfo' (errno: 150)

 

Solution:

Disable the foreign key constraint before userinfo is created. After userinfo is created, enable the foreign key constraint again.

 

Set foreign_key_checks = 0; // disable the foreign key check.

 

Create Table userinfo

(

Id int not null primary key auto_increment,

User_id int not null,

......,

Constraint fk_user_id foreign key (user_id) References users (ID)

) Engine = InnoDB;

 

 

Set foreign_key_checks = 1; // enable the foreign key check.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.