Violation of PRIMARY KEY constraint

Hi I have a table named test and a column named technologyid. Now this column is the primary key of the table
but at the same time, this column is also the foreign key referencing to another table. I know this is weird but this is how the table has been designed.

Now, I have a requirement to insert some records and these are existing technologyid's and it throws the below error.

Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'PK_TechnologyAttributes'. Cannot insert duplicate key in object 'dbo.TechnologyAttributes'. The duplicate key value is (666).
The statement has been terminated.

Is there any way to get around this ?

Many Thanks
gvamsimbaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
That's not weird. Why would you want to have duplicate values in technologyid? Wouldn't that mess your existing queries up?

If you absolutely need to do this, then you need a different (or a composite) Primary Key.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>Cannot insert duplicate key in object 'dbo.TechnologyAttributes'.. The duplicate key value is (666).
Please do two things for us:
   Copy-paste the insert T-SQL into this question.
   Click on table test in SSMS, then right-click > Script Table As > Create > New Query editor, and copy-paste the resulting T-SQL into this question.

Also spell out for us why you wish to insert a value into a PK table when it already exists, and based on the above T-SQL we can suggest a workaround.

Another possibility:  Since you described table test, and the error message shows table TechnologyAttributes, maybe there's a trigger on table Test that attempts to insert into table TechnologyAttributes
0
gvamsimbaAuthor Commented:
Hi Phillip,

No, we already have a technology table in which the technologyid is the primary key....

this test table should ideally have its own primary key but for some reason they made the technologyid
as the primary key. We need to have two technlogyid's in this table now for some technologies as we are introducing a new flag column for this test table and the stored proc can work out to make sure only one technology can be returned at any one time.

so what is the best way to achieve this ?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

gvamsimbaAuthor Commented:
HI Jim,

Yes you are right. the table name actually is TechnologyAttributes. we already have a technology table in which the technologyid is the primary key....

INSERT INTO [TechnologyAttributes]
           ([TechnologyID]
           ,[FuelBurningFlag]
           ,[StartDate]
           ,[CapacityFactor]
           ,[MonthsToPreliminaryExpiry])
     VALUES
           (666
           ,0
           ,'2014-04-01 00:00:00'
           ,0
           ,9)
GO


CREATE TABLE [dbo].[TechnologyAttributes](
      [TechnologyID] [int] NOT NULL,
      [FuelBurningFlag] [bit] NOT NULL,
      [StartDate] [smalldatetime] NOT NULL,
      [EndDate] [smalldatetime] NULL,
      [TechnologyClassID] [int] NULL,
      [CapacityFactor] [tinyint] NOT NULL,
      [MonthsToPreliminaryExpiry] [int] NOT NULL,
 CONSTRAINT [PK_TechnologyAttributes] PRIMARY KEY CLUSTERED
(
      [TechnologyID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Maps 1-to-1 with Technology ID in Technology View' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TechnologyAttributes', @level2type=N'COLUMN',@level2name=N'TechnologyID'
GO

ALTER TABLE [dbo].[TechnologyAttributes]  WITH CHECK ADD  CONSTRAINT [FK_TechnologyAttributes_Technology_TechnologyID] FOREIGN KEY([TechnologyID])
REFERENCES [dbo].[Technology] ([TechnologyID])
GO

ALTER TABLE [dbo].[TechnologyAttributes] CHECK CONSTRAINT [FK_TechnologyAttributes_Technology_TechnologyID]
GO

ALTER TABLE [dbo].[TechnologyAttributes] ADD  CONSTRAINT [DF_TechnologyAttributes_CapacityFactor]  DEFAULT ((100)) FOR [CapacityFactor]
GO

ALTER TABLE [dbo].[TechnologyAttributes] ADD  DEFAULT ((-1)) FOR [MonthsToPreliminaryExpiry]
GO
0
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
>>We need to have two technlogyid's in this table now for some technologies as we are introducing a new flag column for this test table

Then can't you have (as I was suggesting earlier) a composite PK, i.e. technologyid and flag?
0
gvamsimbaAuthor Commented:
Hi Phillip,

How do I do that ?
0
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
drop your existing PK, and then

alter table test
add constraint PKTest primary key(technlogyid,[Flag])
0
gvamsimbaAuthor Commented:
oh Got you Phillip, what you are saying is, instead of creating a new Primary key column to this table, we would just drop the existing PK on the technologyid column and then add a new column which is a flag.
and then create a new composite primary key on technlogyid column and flag column which will make sure that
the combination values in both these columns are never the same ?

Is it right ?
0
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Well, you are saying that you have or are going to add a new column which is a flag, and then presumably you want the combination values in both columns to be never the same, so you can have:

TechnologyID Flag
665                   True
666                   True
666                   False
667                   True

If that is so, then yes.
0
gvamsimbaAuthor Commented:
Yes that is right , those combination values should never be the same. this is a look up table anyway...

But how many Indexes will this create now ?

clustered or non-clustered ?
0
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Should only be the same number of indexes - you are dropping one PK and creating another.

And PKs by default should be clustered, but if you have a reason for non-clustered...
0
gvamsimbaAuthor Commented:
But I thought clustered index can only be on one column...is it not right ?
0
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
No - you can't have more than one clustered index per table, but a clustered index can be over more than one column.

For example, this code, which creates a table then adds a composite clustered index, works:

CREATE TABLE [dbo].[Salads](
	[ID] [int] NOT NULL,
	[name] [nvarchar](10) NULL,
	[F1] [nvarchar](10) NULL,
	[F2] [nvarchar](10) NULL,
	[F3] [nvarchar](10) NULL)
GO

CREATE CLUSTERED INDEX IX_Salads 
    ON dbo.Salads (ID, [name]); 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gvamsimbaAuthor Commented:
Excellent...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.