当表存在时,获取“外键引用无效表”错误

时间:2013-10-17 16:40:19

标签: sql foreign-keys sql-server-2012

我有三张桌子

Create table PROPERTY(
    property_number     int,
    property_name       int,
    property_type       varchar(30),
    property_street     varchar(30),
    property_city       varchar(30),
    property_zip        int,
    constraint PK_PROPRETY_NUMBER primary key(property_number)
)

Create table SERVICE(
    service_ID              int,
    service_description     varchar(150),
    service_amount          money,
    constraint PK_SERVICE_ID primary key(service_ID),
)

Create table SERVICEHISTORY(
    service_ID          int,
    service_date        date,
    property_number     int,
    constraint PK_SERVICEHISTORY_ID primary key(service_ID, service_date),
    constraint FK_SERVICE_ID foreign key(service_ID)
            references SERVICE(service_ID),
    constraint FK_PROPERTY_NUMBER foreign key(property_number)
            references PROPERTY(property_number)
)

我成功运行前两个,它们出现在“Tables”下的Object Explorer中。在SERVICEHISTORY中,“引用SERVICE”和“PROPERTY”是错误的。当我运行该脚本时,我得到:

Msg 1767, Level 16, State 0, Line 11
Foreign key 'FK_SERVICE_ID' references invalid table 'SERVICE'.
Msg 1750, Level 16, State 0, Line 11
Could not create constraint. See previous errors.

这里发生了什么?

0 个答案:

没有答案