Question

Hey guys I've created a database from this script below. And I am adding a table for State Code and trying to reference GTEDatabaes.State_ID as a foriegn key but I'm getting this error. I can't even drop the constraint because each time I execute the query, it generates a new name. I've posted the script below with inserts removed for easier reading. Thank you I'm using 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'.

Here's the Script:

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
Was it helpful?

Solution

You didn't see FK in scripts because there's error occurs while your attempting to create FK constrains.

An FK constraint requires that each values in the reference column in source table must exists in the referenced column of target column.

In this case, it could be that some data exists in GTEDatabases table while State_Code is empty. So, while attempting to create FK, db engines will check if all State_id values in GTEDatabases could be found in State_id column in State_Code table, and it will refuse to complete the operation when check fails.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top