Mysql processing to add a foreign key prompt error 150 solution _mysql

Source: Internet
Author: User

An error like this cannot create a. frm file seems to imply incorrect permissions or other reasons for operating system files, but in reality, this MySQL error has been reported as a bug in MySQL itself and has been in the MySQL developer list for many years, Yet this seems another misleading.

In many instances, this error occurs because MySQL has always been unable to support the relationship of the problem, and, unfortunately, it does not indicate exactly which one of the problems will lead to the above kind of error, below I put the cause of this terrible 150 common causes of the error listed, And I sort of the size of the probabilities

Known reasons:

1, two fields of type or size do not match strictly, for example, if an int (10), then the foreign key must also be set to int (10), not int (11) nor can it be TINYINT. You have to use the show command to see the size of the field, because some query browsers sometimes display int (10) and int (11) as integers. In addition, you must also determine whether two fields are one signed and the other is unsigned, which must be closely matched, and for more information about signed and unsigned, see: http://www.verysimple.com/blog/?p=57

2, one of the foreign keys you are trying to reference is not indexed, or is not a primary key, and if one of them is not a primary key, you must create an index for it.

3, the name of the foreign key is an already existing key value, this time, you should check your database to ensure that the name is unique, or you add a few random characters after the key name to test whether this is the reason.

4, one or two of the tables are MyISAM engine tables, if you want to use a FOREIGN key constraint, you must be a InnoDB engine, (in fact, if two tables are MyISAM engines, this error does not occur at all, but it does not produce foreign keys), you can set the engine type of the table by querying the browser

5, you may have set the on DELETE set NULL, but the associated key field is set to the NOTS null value. You can fix the bug by modifying the Cascade property value or by setting the field property to allow null.

6, make sure your charset and Collate options are consistent at the table level and at the field level

7, you may set a default value for the foreign key, such as Default=0

8, in this relationship, one of the fields is one of the mixed key values, which does not have its own independent index, at which point you must create a separate index for it.

9, there are grammatical errors in the ALTER declaration

1, foreign key reference type is not the same, the primary key is int foreign key is char

2, the column referenced in the main table could not be found

3, the primary key and the foreign key character encoding inconsistent

4. If you want to establish a foreign key, you must first establish an index. There is also an error if no indexing is established.

My problem solution is to add the following sentence to the SQL ok! successfully import the script
Engine=innodb DEFAULT Charset=utf8;
CharSet corresponds to the character set of your primary key table!

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.