用pd设计好表后,并且创建了主键和外键关系,
表结构如下
/*==============================================================*/
/* Table: ResourceSubTypes                                      */
/*==============================================================*/
create table ResourceSubTypes (
   ResourceSubTypeID    nvarchar(36)         not null,
   ResourceSubTypeDescription nvarchar(50)         null,
   LockedBy             nvarchar(36)         null,
   LockTS               datetime             null,
   CompanyID            nvarchar(36)         not null,
   DivisionID           nvarchar(36)         not null,
   DepartmentID         nvarchar(36)         not null,
   ResourceTypeID       nvarchar(36)         null    ----外键关系
)
goalter table ResourceSubTypes
   add constraint PK_RESOURCESUBTYPES primary key  (ResourceSubTypeID, CompanyID, DivisionID, DepartmentID)
go/*==============================================================*/
/* Table: ResourceSubTypes                                      */
/*==============================================================*/
create table ResourceSubTypes (
   ResourceSubTypeID    nvarchar(36)         not null,
   ResourceSubTypeDescription nvarchar(50)         null,
   LockedBy             nvarchar(36)         null,
   LockTS               datetime             null,
   CompanyID            nvarchar(36)         not null,
   DivisionID           nvarchar(36)         not null,
   DepartmentID         nvarchar(36)         not null,
   ResourceTypeID       nvarchar(36)         null
)
goalter table ResourceSubTypes
   add constraint PK_RESOURCESUBTYPES primary key  (ResourceSubTypeID, CompanyID, DivisionID, DepartmentID)
go
--添加外键
alter table ResourceSubTypes
   add constraint FK_ResType_ResSub_ foreign key (ResourceTypeID)
      references ResourceTypes (ResourceTypeID)
go
当我将这些脚本在sql2005执行的时候出现了
Msg 1776, Level 16, State 0, Line 2
There are no primary or candidate keys in the referenced table 'ResourceSubTypes' that match the referencing column list in the foreign key 'FK_ResSub_Res'.
我怀疑是CompanyID, DivisionID, DepartmentID作为主键的问题,因为在两个表中,并不存在他们三个字段的关系,
请问要如何修改,谢谢

解决方案 »

  1.   

    请将ResourceTypes表的ResourceTypeID设为主键
      

  2.   


    /*==============================================================*/
    /* Table: ResourceTypes                                      */
    /*==============================================================*/
    create table ResourceTypes (
       ResourceSubTypeID    nvarchar(36)         not null,
       ResourceSubTypeDescription nvarchar(50)         null,
       LockedBy             nvarchar(36)         null,
       LockTS               datetime             null,
       CompanyID            nvarchar(36)         not null,
       DivisionID           nvarchar(36)         not null,
       DepartmentID         nvarchar(36)         not null,
       ResourceTypeID       nvarchar(36)         not null  --主键
    )
    goalter table ResourceTypes
       add constraint PK_RESOURCESUBTYPES primary key  (ResourceTypeID)
    go/*==============================================================*/
    /* Table: ResourceSubTypes                                      */
    /*==============================================================*/
    create table ResourceSubTypes (
       ResourceSubTypeID    nvarchar(36)         not null,
       ResourceSubTypeDescription nvarchar(50)         null,
       LockedBy             nvarchar(36)         null,
       LockTS               datetime             null,
       CompanyID            nvarchar(36)         not null,
       DivisionID           nvarchar(36)         not null,
       DepartmentID         nvarchar(36)         not null,
       ResourceTypeID       nvarchar(36)         null    --外键
    )
    goalter table ResourceSubTypes
       add constraint PK_RESOURCESUBTYPESdfd primary key  (ResourceSubTypeID, CompanyID, DivisionID, DepartmentID)
    go
    --添加外键
    alter table ResourceSubTypes
       add constraint FK_ResType_ResSub foreign key (ResourceTypeID)
          references ResourceTypes (ResourceTypeID)
    go