MySQL won't let me add multiple foreign keys

自作多情 提交于 2019-12-08 11:54:31

问题


So I'm working on a project in school in which we use MySQL to make a database and complete various tasks about school subjects and exam boards. One of my questions was to:

"Create and populate a third table called entries, again using query scripts. This table should contain foreign keys to allow sensible links to be made with the other two tables, together with the dates of each exam."

When carrying out this task I tried this code out

CREATE TABLE IF NOT EXISTS entries(

subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT,

subject_name VARCHAR(20) NOT NULL,

level_of_entry VARCHAR(10) NOT NULL,

exam_board VARCHAR(10) NOT NULL,

date_of_exam DATETIME NOT NULL,

PRIMARY KEY (date_of_exam),

FOREIGN KEY (subject_id) REFERENCES subjects(subject_id)

);

And that worked fine, but when I tried this:

CREATE TABLE IF NOT EXISTS entries(

subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT,

subject_name VARCHAR(20) NOT NULL,

level_of_entry VARCHAR(10) NOT NULL,

exam_board VARCHAR(10) NOT NULL,

date_of_exam DATETIME NOT NULL,

PRIMARY KEY (date_of_exam),

FOREIGN KEY (subject_id) REFERENCES subjects(subject_id),

FOREIGN KEY (subject_name) REFERENCES subjects(subject_name)

);

It threw up and error message stating:

"ERROR 1215 (HY000): Cannot add foreign key constraint"

Any clues on how to add multiple foreign key statements without it causing an error. Also I did try using the ALTER TABLE function and that didn't work,

Many thanks

Andrew.

来源:https://stackoverflow.com/questions/36478190/mysql-wont-let-me-add-multiple-foreign-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!