MySQL Error 1215 (HY000): Cannot add foreign KEY constraint resolution when you execute a SQL file with a foreign key

Source: Internet
Author: User

ERROR 1215 (HY000): Cannot add foreign KEY constraint

Recently encountered this error in the construction of the table, and then found the next find a solution, record:

Two tables were originally to be built:

1234567891011121314 createtabledepartment(id int,namevarchar(20));createtableemployee(id intprimarykeyauto_increment,namevarchar(20),sex enum(‘male‘,‘female‘notnulldefault‘male‘,age int,dep_id int,foreignkey(dep_id) referencesdepartment(id));

The following prompt appears:

Then began to surf the internet, and some said that the engine was used for different reasons, looked under mine, the two engines are identical,

Some say one is int, because after adding auto_increment, actually become int undesigned, since become

int undesigned that I also set into undesigned, and then insert, error Still,

At last I thought of the ID in the department table above, just shaping, probably not unique,

and added primary key.

1234 createtable department(id int primary key,namevarchar(20));

Test again, found to be able to create the table normally

The specific principle here is not clear, the above is only speculation, hereby recorded

If you find a reason to update!

Update:

Https://stackoverflow.com/questions/22013511/mysql-error-1215-hy000-cannot-add-foreign-key-constraint

That is to say, my above guess is correct!

Take a look at the unique test:

Transferred from: http://www.cnblogs.com/Andy963/p/7634537.html

The last thing to note is that the foreign key must be the primary key or the unique key of the other table (that is, either set the column as the primary key or create a unique index)

PowerDesigner establish a primary key no longer described, the steps to establish a unique index are as follows

Select a column as unique index.

As for the establishment of foreign keys as follows:

Double-click the connector between the table and the tables

MySQL Error 1215 (HY000): Cannot add foreign KEY constraint resolution when you execute a SQL file with a foreign key

Related Article

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.