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)