Skip to content
Advertisement

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

I have read Database system concepts, 6th edition, Silberschatz. I’m going to implement the university database system shown in chapter 2 on OS X on MySQL. But I have a trouble with creating the table course. the table department looks like

Creating the table course causes the following error.

after searching google for foreign key constraint, I have just learned that the word ‘foreign key constraint’ indicates that data from foreign key column in the table course must exist in primary key column in the table department. But I should have met this error when inserting data.

If not, why does author make me execute that SQL statement?

If I really execute erroneous SQL statement, Does I have to designate dept_name in course table as foreign key after inserting some data?

EDIT : typing set foreign_key_checks=0 into mysql> does not fix the error.

Advertisement

Answer

The syntax of FOREIGN KEY for CREATE TABLE is structured as follows:

So your MySQL DDL should be:

Also, in the department table dept_name should be VARCHAR(20)

More information can be found in the MySQL documentation

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement