Globby
Globby

Reputation: 61

foreign key references invalid table. BUT WHY

CREATE TABLE Bookings
(
    BookingID CHAR(10) CONSTRAINT pkBookinglD PRIMARY KEY,
    BookingName CHAR(30), 
    Price INT,
    Catergory CHAR(30),

    RoomID CHAR(10) 
        CONSTRAINT fkRoomID FOREIGN KEY REFERENCES Rooms(RoomID)
)

Error message:

Foreign key fkRoomID references invalid table Rooms.

How do I fix this? I have the table 'Rooms' but it keeps saying invalid table.

This is my rooms table

CREATE TABLE Rooms
(
    RoomID CHAR(10) CONSTRAINT pkRoomlD PRIMARY KEY,
    RoomType CHAR(30), 
    Price INT, 
    FloorNumber INT
)

Upvotes: 2

Views: 6358

Answers (2)

JBarros
JBarros

Reputation: 81

Check that the correct database is selected in the "Available databases" drop-down control.

Upvotes: 1

S.Jose
S.Jose

Reputation: 246

If you are creating the tables using single script, script for creating Rooms table should be on top and then for table 'Bookings'. Another option is add all the tables with Primary key at first, then Alter table and add foreign key constraint:

ALTER TABLE Bookings
    ADD CONSTRAINT fkRoomID FOREIGN KEY(RoomID) REFERENCES Rooms(RoomID)

Upvotes: 4

Related Questions