Skip to content
Advertisement

Reservation table query

when I execute my reservation table query, I get an error.

Any help?

This is my reservation table query

CREATE TABLE RESERVATION 
(
    NUMCHAMBRE INT FOREIGN KEY REFERENCES CHAMBRE (NUMCHAMBRE) ,
    NUMHOTEL INT FOREIGN KEY REFERENCES HOTEL (NUMHOTEL),
    NUMCLIENT INT FOREIGN KEY  REFERENCES CLIENT (NUMCLIENT),
    DATEARRIVE DATE,
    DATEDEPART DATE, 

    PRIMARY KEY (NUMHOTEL, NUMCLIENT, DATEARRIVE)
);

This is the error I get:

Msg 1776, Level 16, State 0, Line 2
There are no primary or candidate keys in the referenced table ‘CHAMBRE’ that match the referencing column list in the foreign key ‘FK__RESERVATI__NUMCH__2BFE89A6’.

Msg 1750, Level 16, State 0, Line 2
Could not create constraint or index. See previous errors.

Advertisement

Answer

According to your comment, the primary key on chambre is composite. So the foreign key reference needs to include all the columns:

CREATE TABLE RESERVATION (
    NUMCHAMBRE int,
    NUMHOTEL int Foreign Key REFERENCES HOTEL (NUMHOTEL),
    NUMCLIENT int Foreign Key  REFERENCES CLIENT (NUMCLIENT),
    DATEARRIVE date,
    DATEDEPART date, 
    foreign key (numhotel, numchambre) references chambre (numhotel, numchambre);
)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement