0

I don't really get programming much so sorry if my question looks cliched. I am getting the "Error code: 1215 : Cannot add foreign key constrain MYSql" whenever I try this program

create table department(dept_name varchar(20) primary key,
building varchar(15),
budget numeric(12,2));
create table course(course_id varchar (7),
title varchar (50),
dept_name varchar (20),
credits numeric (2,0),
primary key (course_id),
foreign key (dept_name) references department);

Can somebody help me out

KHKR
  • 31
  • 5
  • What is the logic of adding a foreign key to the same table which is a primary key ? – Abhik Chakraborty Mar 07 '15 at 09:50
  • This question has been asked many times here , check it : http://stackoverflow.com/questions/24663595/mysql-error-code-1215-cannot-add-foreign-key-constraint – Sagar Joon Mar 07 '15 at 09:59
  • possible duplicate of [MySQL Error 1215: Cannot add foreign key constraint](http://stackoverflow.com/questions/16969060/mysql-error-1215-cannot-add-foreign-key-constraint) – georstef Mar 07 '15 at 10:09

1 Answers1

0

You have not defined which column of dept_name you are referencing. Please try this:

create table department(dept_name varchar(20) primary key,
building varchar(15),
budget numeric(12,2));
create table course(course_id varchar (7),
title varchar (50),
dept_name varchar (20),
credits numeric (2,0),
primary key (course_id),
foreign key (dept_name) references department(dept_name));
Abhishekh Gupta
  • 6,206
  • 4
  • 18
  • 46