Mysql foreign key creation Problems

Source: Internet
Author: User

Mysql foreign key creation problem mysql foreign key creation error 150 Mysql error: error Code: 1005. can't create table 'joblog. SC '(errno: 150) create table SC (id int (10) unsigned not null auto_increment comment 'id', Sno int (10) unsigned not null default '0' comment 'student ID ', Cno int (10) unsigned not null default '0' comment 'course No.', Grade int (10) unsigned default null comment 'score ', primary key (id), constraint FK_ SC _1 foreign key (Sno) ref Erences student (id), constraint FK_ SC _2 foreign key (Cno) references course (id) engine = InnoDB default charset = gb2312; the reason is the type (foreign key) of the message_demo table) the attribute is different from the id (primary key) definition of the message_type table. Since I created a foreign key relationship after creating the table, I added an unsigned attribute to the type field, the id field is not added, so an error is returned. The foreign key definition of the "foreign key constraint" complies with the following conditions: · all tables must be of the InnoDB type and cannot be temporary tables. · In the referenced table, an index is required, and the foreign key column is listed in the same order as the first column. If such an index does not exist, it must be automatically created in the reference table. · In the referenced table, there must be an index. The referenced columns are listed in the same order as the first column. · The index prefix of foreign key columns is not supported. One of the consequences is that BLOB and TEXT columns are not included in an external key, because the index of these columns must always contain a prefix length. · If CONSTRAINTsymbol is given, it must be unique in the database. If it is not given, InnoDB automatically creates this name.. Foreign key foreign key enhancement provides a number of benefits for database developers: · assuming that the association design is appropriate, foreign key constraints make it more difficult for programmers to introduce inconsistency into the database. · The database server has a centralized constraints check function, so it is not necessary to perform such checks on the application side. This eliminates the possibility that different applications use different methods to check constraints. · Use cascading update and deletion to simplify application code. · Designing appropriate foreign keys helps to document the relationships between tables. Remember that these benefits come at the cost of the additional overhead required by the database server to perform the necessary checks. Additional server checks may affect performance. For some applications, this feature is not welcome and should be avoided as much as possible. (For this reason, foreign key logic is implemented at the application level in some major commercial applications ).

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.