Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i tried to create tables as per given in my textbook in MySQL server as follows:

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

create table course
(course_id      varchar(8), 
 title          varchar(50), 
 dept_name      varchar(20),
 credits        numeric(2,0),
 primary key (course_id),
 foreign key (dept_name) references department
);

the department table gets created, but an error

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

is displayed.

The queries seem very simple. And i am unable to find any mistake. What can be the possible error that is preventing me to create the table?

share|improve this question
up vote 1 down vote accepted

You are missing column name of parent table to which your child refer, while creating child table, do like below

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

create table course
(course_id      varchar(8), 
 title          varchar(50), 
 dept_name      varchar(20),
 credits        numeric(2,0),
 primary key (course_id),
 foreign key (dept_name) references department(dept_name)
);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.