SQL Server表创建期间的外键约束错误

时间:2013-12-18 16:55:27

标签: sql sql-server-2008 foreign-keys ddl

我正在尝试在SQL Server上创建一个具有外键约束的表。我检查了所有父表,并且它们的主键与我的新外键约束匹配。有人可以帮助我解决这个问题。我的DDL和错误信息如下。

DDL

CREATE TABLE I_IPV_LOB_PROG_PROV_MO_METRIC_TRNS(
    AARP_ORG_ID           int               NOT NULL,
    LOB_ID                int               NOT NULL,
    PGM_CAT_ID            int               NOT NULL,
    PGM_ID                int               NOT NULL,
    PROV_ID               int               NOT NULL,
    CAT_OF_MEAS_ID        int               NOT NULL,
    SUBCAT_OF_MEAS_ID     int               NOT NULL,
    MEAS_ID               int               NOT NULL,
    TYPE_OF_METRIC_ID     int               NOT NULL,
    METRIC_VAL_ROLE_ID    int               NOT NULL,
    MO_ID                 int               NOT NULL,
    OPER_TXT              varchar(5)        NULL,
    METRIC_VAL            decimal(19, 2)    NOT NULL,
    LST_UPD_DT            datetime          NOT NULL,
    LOAD_DT               datetime          NULL,
    LST_UPD_USERID        char(20)          NOT NULL,
    CONSTRAINT PK79 PRIMARY KEY NONCLUSTERED (TYPE_OF_METRIC_ID, METRIC_VAL_ROLE_ID, AARP_ORG_ID, LOB_ID, PGM_CAT_ID, PGM_ID, PROV_ID, MEAS_ID, MO_ID, CAT_OF_MEAS_ID, SUBCAT_OF_MEAS_ID), 
    CONSTRAINT RefI_IPV_TYPE_OF_METRIC_TRNS73 FOREIGN KEY (TYPE_OF_METRIC_ID)
    REFERENCES I_IPV_TYPE_OF_METRIC_TRNS(TYPE_OF_METRIC_ID),
    CONSTRAINT RefI_IPV_METRIC_VAL_ROLE_TRNS75 FOREIGN KEY (METRIC_VAL_ROLE_ID)
    REFERENCES I_IPV_METRIC_VAL_ROLE_TRNS(METRIC_VAL_ROLE_ID),
    CONSTRAINT RefI_IPV_LOB_PROG_PROV_MEAS_TRNS345 FOREIGN KEY (AARP_ORG_ID,LOB_ID,PGM_CAT_ID,PGM_ID,PROV_ID,MEAS_ID,CAT_OF_MEAS_ID,SUBCAT_OF_MEAS_ID)
    REFERENCES I_IPV_LOB_PROG_PROV_MEAS_TRNS(AARP_ORG_ID,LOB_ID,PGM_CAT_ID,PGM_ID,PROV_ID,MEAS_ID,CAT_OF_MEAS_ID,SUB_CAT_OF_MEAS_ID)
)
go

父表DDL

CREATE TABLE I_IPV_LOB_PROG_PROV_MEAS_TRNS(
    AARP_ORG_ID          int         NOT NULL,
    LOB_ID               int         NOT NULL,
    PGM_CAT_ID           int         NOT NULL,
    PGM_ID               int         NOT NULL,
    PROV_ID              int         NOT NULL,
    CAT_OF_MEAS_ID       int         NOT NULL,
    SUBCAT_OF_MEAS_ID    int         NOT NULL,
    MEAS_ID              int         NOT NULL,
    LOAD_DT              datetime    NULL,
    LST_UPD_USERID       char(20)    NOT NULL,
    LST_UPD_DT           datetime    NOT NULL,
    CONSTRAINT PK115 PRIMARY KEY NONCLUSTERED (MEAS_ID, PROV_ID, AARP_ORG_ID, LOB_ID, PGM_ID, PGM_CAT_ID, CAT_OF_MEAS_ID, SUBCAT_OF_MEAS_ID), 
    CONSTRAINT RefI_IPV_LOB_PROG_CAT_PROV_TRNS322 FOREIGN KEY (PROV_ID, AARP_ORG_ID, LOB_ID, PGM_ID, PGM_CAT_ID)
    REFERENCES I_IPV_LOB_PROG_CAT_PROV_TRNS(PROV_ID, AARP_ORG_ID, LOB_ID, PGM_ID, PGM_CAT_ID),
    CONSTRAINT RefI_IPV_MEAS_CAT_TRNS342 FOREIGN KEY (MEAS_ID, CAT_OF_MEAS_ID, SUBCAT_OF_MEAS_ID)
    REFERENCES I_IPV_MEAS_CAT_TRNS(MEAS_ID, CAT_OF_MEAS_ID, SUBCAT_OF_MEAS_ID)
)
go

错误消息

  

Msg 1776,Level 16,State 0,Line 1
  引用表'I_IPV_LOB_PROG_PROV_MEAS_TRNS'中没有主键或候选键与外键'RefI_IPV_LOB_PROG_PROV_MEAS_TRNS345'中的引用列列表匹配。
  Msg 1750,Level 16,State 0,Line 1
  无法创建约束。查看以前的错误。

1 个答案:

答案 0 :(得分:0)

目标表中的一个字段必须是其主键(最佳),或者使用您列出的至少一个字段通过某个索引可以非常快速地找到它们。如果列表中没有键,则必须读取整个表以查找特定行,这不是一件好事,所以不会。 在目标表上创建一个唯一的索引,其中包含您正在约束的一些/所有字段,它应该没问题。