MySQL Create foreign key error (Note database table field sort)

Source: Internet
Author: User
Tags mysql create

1. The type or size of the two fields does not match exactly. For example, if one is an int (10), then the foreign key must also be set to int (10), not int (11), or tinyint. In addition, you must determine whether the two fields are one signed, and the other is unsigned (i.e. unsigned), which must match exactly, more information about signed and unsigned, see:/http www.verysimple.com/blog/?p=57

2. The field attempting to set the foreign key is not indexed, or is not a primary key (primary key). If one of these is not primary key, you must first create an index for it.

3. One or two of these tables is a table of the MyISAM engine. If you want to use a FOREIGN key constraint, the table must be the InnoDB engine (in fact, if two tables are MyISAM engines, this error does not occur at all, but it does not generate foreign keys, only indexes) you need to check the engine type of the table.

4. The name of the foreign key cannot be duplicated. You should check your database to make sure that the name is unique, or you should add a few random characters after the key name to test if this is the reason.

5. You may have set on DELETE set NULL, but the field of the related key is also set to nots null value. You can resolve this by modifying the property value of the Cascade or by setting the field property to allow null.

6. Make sure that your charset and collate options are consistent at the table level and at the field level.

7. You may have set a default value for the foreign key, such as default=0.

8. A syntax error in the ALTER declaration

9. Make sure the Collate collation!

MySQL Create foreign key error (Note database table field sort)

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.