0
create table department
(
dept_name varchar(20), 
building varchar(20), 
budget numeric(12, 2), 
primary key(dept_name)
);

This is table that i created before...

now i am writing this:

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
);

an it shows :

ERROR 1005 (HY000): Can't create table 'satyarth.course' (errno: 150)

Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
Satylogin
  • 3
  • 1
  • 2
  • Possible duplicate of [this](https://stackoverflow.com/questions/9018584/error-code-1005-cant-create-table-errno-150) SO question. – Jeroen Heier Jul 08 '17 at 07:31

2 Answers2

1

You're missing a portion of the FOREIGN KEY requirement, as you cannot simply refer to another table:

foreign key (dept_name) references department(dept_name)
Paul T.
  • 4,703
  • 11
  • 25
  • 29
1

Add the reference table field name PFB

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`)    
);
Palani Samy
  • 169
  • 9