外键没有添加到预订表,任何人都可以发现问题

时间:2017-11-21 08:56:42

标签: sql oracle

  Create table Hotel (
    Hotel_no varchar (10) NOT NULL,
    Hotel_name varchar (10) NOT NULL,
    Address varchar (600) NOT NULL,
    Primary key (Hotel_no) );


    Create table Room (
    Room_no varchar (10) NOT NULL,
    Hotel_no varchar (10) NOT NULL,
    type varchar (40) NOT NULL,
    price varchar (20) NOT NULL,
    Primary key (Room_no, Hotel_no) );


    Create table Guest (
    Guest_no varchar (10) NOT NULL,
    Guest_name varchar (10) NOT NULL,
    Address varchar (600) NOT NULL,
    Primary key (Guest_no) );


    Create table Booking (
    date_from date,
    date_to date,
    Hotel_no varchar (10) NOT NULL,
    Guest_no varchar (10) NOT NULL,
    Room_no varchar (10) NOT NULL,
    Primary key (date_from, Hotel_no, Guest_no),
    Foreign key(Room_no)references Room(Room_no));

当我尝试将外键添加到预订表时,它会出现ORA-02270错误,我似乎无法弄清楚问题 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

Room上的外键应引用主键Room(Room_no, Hotel_no)

答案 1 :(得分:0)

如果主键是一组列,则外键也应该是与复合键列对应的一组列。 请尝试以下代码:

Create table Booking ( date_from date, date_to date, Hotel_no varchar (10) NOT NULL, Guest_no varchar (10) NOT NULL, Room_no varchar (10) NOT NULL, Primary key (date_from, Hotel_no, Guest_no), Foreign key(Room_no,Hotel_no)references Room(Room_no,Hotel_no);