Problems encountered while adding foreign keys

Source: Internet
Author: User

In MySQL beginner, encountered this problem.

Existing two tables, student and class. The student table has the ID as the primary key, the name and the Classid,class table have the ID as the primary key, and the name is ready to set the ClassID as the foreign KEY constraint. The statements are as follows

ALTER TABLE student ADD constraint Fk_stu_classid foreign key (ClassID) references class (ID);

There is no error after checking the statement, but the error message is as follows;

Can ' t create table ' db_19. #sql -153c_4 ' (errno:150)

Depressed for a half day after the definition of the query foreign key, the requirement is the same type as the associated primary key, so the problem is resolved after the modification.

Another situation was found after the query

When this type of error occurs, start with three angles:

1. Make sure the primary table has a primary key.

2, ensure that the master-slave table data engine is InnoDB type.

3. Make sure that the key field type from the table is the same as the primary table .

So this is the wrong thing to happen.

This article is from the "Neruda" blog, make sure to keep this source http://neruda.blog.51cto.com/9652194/1583023

Problems encountered while adding foreign keys

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.