10

I'm trying to setup a MySQL-Script that creates 5 tables. In three tables, there is a FOREIGN KEY, and for all three of them the same error appears:

Error Code: 1072. Key column ... doesn't exist in table

whereas ... are Gebaeude, Dept_Name and Mat_Nr

Here's the script

use cs261_24;

drop table if exists Professor;
drop table if exists Departement;
drop table if exists Gebaeude;
drop table if exists Student;
drop table if exists Pruefung;


CREATE TABLE Gebaeude (
                        Gebaeude    VARCHAR(20) NOT NULL PRIMARY KEY,
                        Hauswart    VARCHAR(20) NOT NULL,
                        Adresse     VARCHAR(20) NOT NULL
)engine = innodb;

CREATE TABLE Professor  (
                        P_ID        INTEGER PRIMARY KEY CHECK (P_ID > 0),
                        P_Name      VARCHAR(20) NOT NULL,
                        Dept_Name   VARCHAR(20) NOT NULL,
                        Raum        INTEGER UNIQUE CHECK (Raum > 0),
                        Tel         INTEGER(10) UNIQUE CHECK (Tel > 210000000),
                        FOREIGN KEY (Gebaeude) REFERENCES Gebaeude (Gebaeude) 
)engine = innodb;

CREATE TABLE Departement (
                        Dept_Name   VARCHAR(20) NOT NULL PRIMARY KEY,
                        Vorsteher   VARCHAR(20) NOT NULL
)engine = innodb;

CREATE TABLE Student (
                        Mat_Nr      INTEGER(8) PRIMARY KEY CHECK (Mat_Nr > 0),
                        S_Name      VARCHAR(20) NOT NULL,
                        Semester    INTEGER CHECK(Semester > 0),
                        FOREIGN KEY (Dept_Name) REFERENCES Departement (Dept_Name) 
)engine = innodb;

CREATE TABLE Pruefung (
                        Pr_ID       INTEGER PRIMARY KEY CHECK(Pr_ID > 0),
                        Fach        VARCHAR(20) NOT NULL,
                        Pruefer     VARCHAR(20) NOT NULL,
                        Note        FLOAT CHECK (Note >= 1 AND Note <= 6),
                        FOREIGN KEY (Mat_Nr) REFERENCES Student (Mat_Nr)
)engine = innodb;

Why? I work with MySQL Workbench, and I clearly see the created tables, plus the specific columns are marked as primary keys!

Valentino Ru
  • 4,964
  • 12
  • 43
  • 78

2 Answers2

16

You are doing it wrong, take a look to this example

http://www.sqlfiddle.com/#!2/a86cf

your FK line should be more like this:

FOREIGN KEY (field_that_will_be_Fk) REFERENCES Table_to_reference (field_to_reference)

I.E=

CREATE TABLE Gebaeude (
                        Gebaeude    VARCHAR(20) NOT NULL PRIMARY KEY,
                        Hauswart    VARCHAR(20) NOT NULL,
                        Adresse     VARCHAR(20) NOT NULL
)engine = innodb;

CREATE TABLE Professor  (
                       Gebaeude_FK varchar(20) NOT NULL,
                        P_ID        INTEGER PRIMARY KEY CHECK (P_ID > 0), 
                        P_Name      VARCHAR(20) NOT NULL,
                        Dept_Name   VARCHAR(20) NOT NULL,
                        Raum        INTEGER UNIQUE CHECK (Raum > 0),
                        Tel         INTEGER(10) UNIQUE CHECK (Tel > 210000000),
                        FOREIGN KEY (Gebaeude_FK) REFERENCES Gebaeude (Gebaeude) 
)engine = innodb;
jcho360
  • 3,724
  • 1
  • 15
  • 24
2

You have to create the column in each of the tables before you can constrain them as a foreign key.

See this link for reference: Link

Community
  • 1
  • 1
Jordan Kaye
  • 2,837
  • 15
  • 15