试图添加FK约束但存在奇怪的FK约束(参见脚本)

时间:2013-07-30 00:13:24

标签: constraints ddl

嘿伙计我已经从下面这个脚本创建了一个数据库。我正在为状态代码添加一个表,并尝试将GTEDatabaes.State_ID作为foriegn键引用,但我收到此错误。我甚至无法删除约束,因为每次执行查询时,它都会生成一个新名称。我已经发布了下面的脚本,删除了插入以便于阅读。谢谢 我正在使用SQL Server 2012。

 create Table State_Code
(State_ID int identity (1,1) primary key,
 State_Name varchar(30))

Alter Table GTEDatabases
ADD foreign key (State_Id)
references State_Code (State_ID)   


Msg 547, Level 16, State 0, Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint          
"FK__GTEDataba__State__1DE57479". The conflict occurred in database "SBAMaster", table     
"dbo.State_Code", column 'State_ID'.

这是脚本:

USE [SBAMaster]
GO
/****** Object:  Table [dbo].[Users]    Script Date: 10/12/2012 11:11:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = 
OBJECT_ID(N'[dbo].[Users]')          AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Users](
[User_Id] [int] IDENTITY(1,1) NOT NULL,
[User_First] [nvarchar](30) NULL,
[User_Last] [nvarchar](30) NULL,
[User_Email] [nvarchar](70) NULL,
[User_Pwd] [nvarchar](8) NULL,
[Phone1] [nvarchar](70) NULL,
[Phone2] [nvarchar](70) NULL,
[Active] [bit] NULL,
[SecurityQuestion1] [int] NULL,
[SecurityAnswer1] [nvarchar](500) NULL,
[SecurityQuestion2] [int] NULL,
[SecurityAnswer2] [nvarchar](500) NULL,
[DateEntered] [smalldatetime] NULL,
[Need_Approval] [bit] NULL,
[Can_Approve] [bit] NULL,
 CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
(
[User_Id] ASC    
)WITH (PAD_INDEX  = OFF, 
STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,  
 ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  ) ON [PRIMARY]
END
GO
SET IDENTITY_INSERT [dbo].[Users] ON
*****Inserts******
GO
SET IDENTITY_INSERT [dbo].[Users] OFF
/****** Object:  Table [dbo].[Roles]    Script Date: 10/12/2012 11:11:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = 
OBJECT_ID(N'[dbo].[Roles]')     AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Roles](
[Role_Id] [int] IDENTITY(1,1) NOT NULL,
[Role_Name] [nvarchar](20) NULL,
[Role_Description] [nvarchar](50) NULL,
 CONSTRAINT [PK_Roles] PRIMARY KEY CLUSTERED 
(
[Role_Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
IGNORE_DUP_KEY = OFF,  ALLOW_ROW_LOCKS  = ON, 
ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET IDENTITY_INSERT [dbo].[Roles] ON
*****Inserts*******
SET IDENTITY_INSERT [dbo].[Roles] OFF
/****** Object:  Table [dbo].[GTEDatabases]    Script Date: 10/12/2012 11:11:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID
(N'[dbo].[GTEDatabases]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[GTEDatabases](
[GteDatabases_Id] [int] IDENTITY(1,1) NOT NULL,
[CustName] [nvarchar](50) NULL,
[Databasename] [nvarchar](50) NULL,
[Active] [bit] NULL,
[DateEntered] [smalldatetime] NULL,
[CustNumber] [nvarchar](30) NULL,
[Address1] [nvarchar](200) NULL,
[Address2] [nvarchar](200) NULL,
[Zip] [nvarchar](15) NULL,
[City_Id] [int] NULL,
[State_Id] [int] NULL,
[Country_Id] [int] NULL,
[Cust_Since] [int] NULL,
[Primary_ContactName] [nvarchar](30) NULL,
[Primary_Email] [varchar](60) NULL,
[Primary_Phone] [nvarchar](20) NULL,
[Secondary_ContactName] [nvarchar](30) NULL,
[Secondary_Email] [varchar](60) NULL,
[Secondary_Phone] [nvarchar](20) NULL,
[Notes] [nvarchar](500) NULL,
[SBASalRep_Email] [nvarchar](60) NULL,
[CSRCon_Email] [nvarchar](60) NULL,
[AllowPO] [bit] NULL,
[CMSCustomer] [bit] NULL,
 CONSTRAINT [PK_GTEDatabases] PRIMARY KEY CLUSTERED 
(
[GteDatabases_Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY =
 OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[GTEDatabases] ON
***Inserts*****
SET IDENTITY_INSERT [dbo].[GTEDatabases] OFF

1 个答案:

答案 0 :(得分:0)

您没有在脚本中看到FK,因为在您尝试创建FK约束时发生错误。

FK约束要求源表中引用列中的每个值必须存在于目标列的引用列中。

在这种情况下,可能是某些数据存在于GTEDatabases表中,而State_Code为空。因此,在尝试创建FK时,db引擎将检查是否可以在State_Code表的State_id列中找到GTEDatabases中的所有State_id值,并且当检查失败时它将拒绝完成操作。