1

我正在创建一个包含一系列表 ddls 的数据库,总共有 13 个表。运行表的 ddl 时出现外键约束错误TBNSOR_VICTIM。ddl 能够创建第一个 fk 约束,但在创建第二个 fk 约束时出现错误。有人可以帮助我或指出脚本中缺少的内容吗?这是运行 ddl 脚本后出现的错误

消息 1776,级别 16,状态 0,第 2 行
被引用的表“dbo.TBNSOR_OFFENSE”中没有与外键“RD09RD03”中的引用列列表匹配的主键或候选键

CREATE TABLE [dbo].[TBNSOR_OFFENSE](
    [RD03_RC27SEQ_NBR] [int] NOT NULL,
    [FK_RD01_RC17SID] [int] NOT NULL,
    [RD03_RC27OFFENSE_DESC] [varchar] (100) NULL,
    [RD03_STATUTE] [char](6) NULL,
    [RD03_RC27PREDATOR_IND] [char] (1) NULL,
    [RD03_RC27CONVICTION_DT] [date] NULL,
    [RD03_RC27CONVICT_CITY] [varchar] (20) NULL,
    [RD03_RC27CONVICT_STATE] [char] (2) NULL,
    [RD03_RC27CONVICT_COUNTY] [varchar] (20) NULL,
    [RD03_RC27CITY] [varchar] (20) NULL,
    [RD03_RC27COUNTY] [varchar] (20) NULL,
    [RD03_RC27OFFENSE_STATE] [char] (2) NULL,
    [RD03_RC27OFFENSE_DATE] [date] NULL,
    [RD03_RC27CONFIN_REL_DT] [date] NULL,
    [RD03_RC27PP_RELEASE_DT] [date] NULL,
 CONSTRAINT [CJ38ID03] PRIMARY KEY CLUSTERED 
(
    [RD03_RC27SEQ_NBR] ASC,
    [FK_RD01_RC17SID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[TBNSOR_OFFENSE]  WITH CHECK ADD CONSTRAINT [RD03RD01] FOREIGN KEY([FK_RD01_RC17SID])
REFERENCES [dbo].[TBNSOR_PHYSCSC] ([RD01_RC17SID])ON DELETE CASCADE
GO

ALTER TABLE [dbo].[TBNSOR_OFFENSE]  CHECK  CONSTRAINT [RD03RD01]
GO

    CREATE TABLE [dbo].[TBNSOR_VICTIM](
    [RD09_RC28SEQ_NUM] [int] NOT NULL,
    [FK_RD01_RC17SID] [int] NOT NULL,
    [FK_RD03_RC27SEQ_NBR] [int] NOT NULL,
    [RD09_RC28SEX] [char] (1) NULL,
    [RD09_RC28RACE] [char] (1) NULL,
    [RD09_RC28AGE] [char](2) NULL,
    [RD09_RC28HIGH_AGE] [char] (2) NULL,
    [RD09_RC28LOW_AGE] [char] (2) NULL,
 CONSTRAINT [CJ38ID09] PRIMARY KEY CLUSTERED 
(
    [RD09_RC28SEQ_NUM] ASC,
    [FK_RD01_RC17SID] ASC,
    [FK_RD03_RC27SEQ_NBR] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[TBNSOR_VICTIM] WITH CHECK ADD CONSTRAINT [RD09RD01] FOREIGN KEY([FK_RD01_RC17SID])
REFERENCES [dbo].[TBNSOR_PHYSCSC] ([RD01_RC17SID])ON DELETE CASCADE
GO

ALTER TABLE [dbo].[TBNSOR_VICTIM] CHECK  CONSTRAINT [RD09RD01]
GO

ALTER TABLE [dbo].[TBNSOR_VICTIM] WITH CHECK ADD CONSTRAINT [RD09RD03] FOREIGN KEY([FK_RD03_RC27SEQ_NBR])
REFERENCES [dbo].[TBNSOR_OFFENSE]([RD03_RC27SEQ_NBR])ON DELETE CASCADE
GO

ALTER TABLE [dbo].[TBNSOR_VICTIM] CHECK CONSTRAINT [RD09RD03]
GO
4

1 回答 1

1

您在 [CJ38TBNSOR_OFFENSE] 表中的两列([RD03_RC27SEQ_NBR] 和 [FK_RD01_RC17SID])上设置了主键,但您只引用其中一列。它应该是:

REFERENCES [cj38].[CJ38TBNSOR_OFFENSE]([RD03_RC27SEQ_NBR],[FK_RD01_RC17SID])
于 2013-03-06T18:41:19.527 回答