为什么sql在创建外键时会显示错误?

时间:2015-11-10 21:05:41

标签: sql sql-server foreign-keys primary-key

我正在尝试将Roomtype primary key作为foreign key插入Room表格,但会向我显示以下错误

  

主要错误0x80040E11,次要错误0

CREATE TABLE Room (
   RoomID nvarchar(8),
   Capacity numeric(3),
   CONSTRAINT FK_TYPE foreign key (Roomtype) references RoomType(Roomtype)
)

Invalid column ID. [ Roomtype ]

CREATE TABLE Room (
    RoomID nvarchar(8) ,
    Capacity numeric(3),
    CONSTRAINT FK_TYPE foreign key (Roomtype) references RoomType(Roomtype)
)

create table RoomType(
    Roomtype nvarchar(2) primary key,
    Description nvarchar(20),
    Responsibility nvarchar(20)
)

1 个答案:

答案 0 :(得分:2)

您需要在Room表中添加roomtype列。您正在为不存在的列创建外键

inputs