A situation with MySQL ERROR 1215 (HY000)

Source: Internet
Author: User

CREATE TABLE Pcourse (
CNO Char (9),
Cpno Char (9),
Primary KEY (Cno,cpno),
Foreign KEY (CNO) References course (CNO),
Foreign KEY (Cpno) References course (CNO)
);
ERROR 1215 (HY000): Cannot add foreign KEY constraint

Computer operation on the west-west database. This foreign key is not to add up. Before student and course because of the Chinese data, so set Character_set_server and set Character_set_database are =utf8, but when the table needs to be defined or need to add the default Charset=utf8. So it should be like this:
CREATE TABLE Pcourse (
CNO Char (9),
Cpno Char (9),
Primary KEY (Cno,cpno),
Foreign KEY (CNO) References course (CNO),
Foreign KEY (Cpno) References course (CNO)
) default Charset=utf8;
Note Defualt Front to have a space to line, otherwise still is error 1215 (HY000): Cannot add foreign KEY constraint.

A foreign key is not required when creating a table course:
CREATE TABLE Course (
CNO Char (9),
CNAME char (*) is not NULL,
Ccredit int,
Primary KEY (CNO)
);
Warning | 1265 | Data truncated for column ' CNAME ' at row 1
This same set of default Charset=utf8 is resolved.

A situation with MySQL ERROR 1215 (HY000)

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.