Solved

find origin of key constraint

Posted on 2013-05-20
2
348 Views
Last Modified: 2013-05-20
I have a constraint on a table

UK_Lien_AssetTaskID

but I cannot find where the constraint is being applied.


Msg 2627, Level 14, State 1, Line 1
Violation of UNIQUE KEY constraint 'UK_Lien_AssetTaskID'. Cannot insert duplicate key in object 'dbo.Lien'. The duplicate key value is (2841370, 1).
The statement has been terminated.


Is there a way to hunt down what is creating this constraint?


For example, I did a "DROP and CREATE" script on the "Lien" table, but I did not see any constraints with:

UK_Lien_AssetTaskID

being referenced:


USE [PP]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Lien_AssetID]') AND parent_object_id = OBJECT_ID(N'[dbo].[Lien]'))
ALTER TABLE [dbo].[Lien] DROP CONSTRAINT [FK_Lien_AssetID]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Lien_AssetTaskID]') AND parent_object_id = OBJECT_ID(N'[dbo].[Lien]'))
ALTER TABLE [dbo].[Lien] DROP CONSTRAINT [FK_Lien_AssetTaskID]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Lien_RoleID]') AND parent_object_id = OBJECT_ID(N'[dbo].[Lien]'))
ALTER TABLE [dbo].[Lien] DROP CONSTRAINT [FK_Lien_RoleID]
GO

IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_Lien_LienSID]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[Lien] DROP CONSTRAINT [DF_Lien_LienSID]
END

GO

USE [PP]
GO

/****** Object:  Table [dbo].[Lien]    Script Date: 05/20/2013 17:32:39 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Lien]') AND type in (N'U'))
DROP TABLE [dbo].[Lien]
GO

USE [PP]
GO

/****** Object:  Table [dbo].[Lien]    Script Date: 05/20/2013 17:32:39 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Lien](
	[LienID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
	[LienSID] [int] NOT NULL,
	[AssetID] [int] NOT NULL,
	[AssetSID] [int] NOT NULL,
	[AssetTaskID] [int] NOT NULL,
	[AssetTaskSID] [int] NOT NULL,
	[LienType] [int] NOT NULL,
	[LienHolder] [varchar](50) NOT NULL,
	[PayOffAmt] [decimal](18, 2) NOT NULL,
	[LienDetails] [varchar](max) NOT NULL,
 CONSTRAINT [PK_Lien] PRIMARY KEY CLUSTERED 
(
	[LienID] ASC,
	[LienSID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 85) ON [PRIMARY],
 CONSTRAINT [UK_Lien_AssetTaskID] UNIQUE NONCLUSTERED 
(
	[AssetTaskID] ASC,
	[AssetTaskSID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 85) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Lien]  WITH NOCHECK ADD  CONSTRAINT [FK_Lien_AssetID] FOREIGN KEY([AssetID], [AssetSID])
REFERENCES [dbo].[Asset] ([AssetID], [AssetSID])
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[Lien] CHECK CONSTRAINT [FK_Lien_AssetID]
GO

ALTER TABLE [dbo].[Lien]  WITH NOCHECK ADD  CONSTRAINT [FK_Lien_AssetTaskID] FOREIGN KEY([AssetTaskID], [AssetTaskSID])
REFERENCES [dbo].[AssetTask] ([AssetTaskID], [AssetTaskSID])
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[Lien] CHECK CONSTRAINT [FK_Lien_AssetTaskID]
GO

ALTER TABLE [dbo].[Lien]  WITH NOCHECK ADD  CONSTRAINT [FK_Lien_RoleID] FOREIGN KEY([LienType])
REFERENCES [dbo].[Lookup] ([LookupID])
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[Lien] CHECK CONSTRAINT [FK_Lien_RoleID]
GO

ALTER TABLE [dbo].[Lien] ADD  CONSTRAINT [DF_Lien_LienSID]  DEFAULT (right(@@servername,(1))) FOR [LienSID]
GO

Open in new window

0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 39182978
lines 60 - 65 in the code posted above shows the creation of the constraint.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 39182985
It sure does.

Thanks for your eagle eyes.  I missed it.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question