1

For some reason, this MySQL fails:

CREATE SCHEMA IF NOT EXISTS `partB` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `partB`;

CREATE TABLE Employees ( ssn CHAR(11),
Name CHAR(30),
mlot INTEGER,
PRIMARY KEY(ssn))
ENGINE = InnoDB;

CREATE TABLE Dept_Mgr ( did INTEGER,
dname CHAR(20),
ssn CHAR(11) NOT NULL,
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES Employees
ON DELETE NO ACTION)
ENGINE = InnoDB;

It gives the error:

ERROR 1005 (HY000): Can't create table partb.dept_mgr (errno: 150)

What can be causing this?

1
  • Hi Rosarch, was this answered to your satisfaction? Commented Feb 26, 2010 at 9:37

3 Answers 3

2

You have to specify the column(s) in the foreign table for the key:

FOREIGN KEY (ssn) REFERENCES Employees (ssn) ...

Sign up to request clarification or add additional context in comments.

Comments

1

This command:

SHOW ENGINE INNODB STATUS;

is your friend when you have trouble creating foreign keys. Output (abridged)

------------------------
LATEST FOREIGN KEY ERROR
------------------------
100225  2:51:42 Error in foreign key constraint of table test/dept_mgr:
FOREIGN KEY (ssn) REFERENCES Employees
ON DELETE NO ACTION)
ENGINE = InnoDB:
Syntax error close to:

ON DELETE NO ACTION)
ENGINE = InnoDB

If you change your statement to:

CREATE TABLE Dept_Mgr ( 
    did INTEGER,
    dname CHAR(20),
    ssn CHAR(11) NOT NULL,
    PRIMARY KEY (did),
    FOREIGN KEY (ssn) REFERENCES Employees(ssn)
) engine = innodb;

it does work.

Comments

0

You only have the foreign key referencing the table, not the column.

Try: references Employee.ssn

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.