0

I am trying to create a series of tables using Foreign Keys of Primary Keys from other tables, however I always get the error

Table Creation Failed: Key column 'PACId' doesn't exist in table:

Here is the first case it appears:

CREATE TABLE PlantAreaCodes(
  PACId INT NOT NULL AUTO_INCREMENT,
  AreaCode INT,
  AreaName CHAR(32),
  Comments TEXT,
  PRIMARY KEY (PACId)
);

CREATE TABLE MajorEquipment(
  MEId INT NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (MEId),
  FOREIGN KEY (PACId) 
  REFERENCES PlantAreaCodes(PACId)
);

Is it to do with the syntax of the foreign key, or is it because the PACId is still empty, and cannot be?

Ben
  • 2,433
  • 5
  • 39
  • 69

2 Answers2

2

It is not that PACId is empty. You haven't declared it in MajorEquipment.

Try using this definition for the second table:

CREATE TABLE MajorEquipment(
  MEId INT NOT NULL AUTO_INCREMENT,
  PACId INT, -- Added this column
  PRIMARY KEY (MEId),
  FOREIGN KEY (PACId) 
  REFERENCES PlantAreaCodes(PACId)
);

Here is a SQL Fiddle.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Try this

CREATE TABLE MajorEquipment(
  MEId INT NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (MEId),
  PACId INT 
  REFERENCES PlantAreaCodes(PACId)
)
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122