avoid duplicates using triggers


A table has no primary key column, Need to write a trigger which should not allow any duplicate values.

more ▼

asked Jan 18, 2010 at 05:54 AM in Default

avatar image

324 49 59 63

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

What is the reason to not have a primary key? Do you really need a trigger?

If you need to prevent duplicates I would recommend you to have a primary key or unique (clustered?) index. Why? If you use a trigger to search for duplicates you will also need an index to get performance. If you don't use an index that match your condition you will end up with a slow table/index/heap scan.

more ▼

answered Jan 18, 2010 at 06:21 AM

avatar image

Håkan Winther
16.5k 36 45 57

  • a trigger is not the way to go here, listen to Håkan's advice.

Jan 18, 2010 at 06:37 AM Matt Whitfield ♦♦
  • the clustered index is the best bet, unless there is a local requirement that you havent mentioned that prevents it.

Jan 18, 2010 at 07:08 AM Fatherjack ♦♦
  • must say your answer was better formulated that mine, and you were faster :-)

Jan 18, 2010 at 11:51 AM Steinar

Thanks Steinar! Better luck next time! :-)

Jan 18, 2010 at 12:32 PM Håkan Winther

A clustered index will require every row to be moved around. A non-clustered unique index (otherwise known as a unique constraint) would be better, and can still be used for Foreign Keys.

Jan 18, 2010 at 08:01 PM Rob Farley
(comments are locked)
10|1200 characters needed characters left

You could also use a unique constraint/unique index. http://msdn.microsoft.com/en-us/library/aa224827(SQL.80).aspx

more ▼

answered Jan 18, 2010 at 06:16 AM

avatar image

1.7k 4 6 10

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Jan 18, 2010 at 05:54 AM

Seen: 2250 times

Last Updated: Jan 18, 2010 at 05:54 AM

Copyright 2016 Redgate Software. Privacy Policy