Q
Problem solve Get help with specific problems with your technologies, process and projects.

Preventing duplicate entries on multiple columns

I have a table in SQL Server with three columns: col1, col2, col3. I have inserted a row with values 1,2,3, and...

it should not repeat again. Through a trigger, how it is possible to restrict a user from inserting the same values? I don't want to create primary keys, or even any identity columns. It should not be allowed to insert 1,2,3, but one could insert 1,2,5 and 1,2,4.


May I suggest a UNIQUE constraint.

alter table yourtable
  add constraint nodupes123
        unique nonclustered
        ( col1, col2, col3 )

You could get the same result using a trigger, but why? In the trigger code, you would have to do a SELECT to see if any row exists already with the particular values for the three columns, and this SELECT is going to be inefficient unless there is an index defined on those three columns. You might as well define the UNIQUE constraint because this creates the index automatically, and then you don't have to write any code.

For More Information


This was last published in January 2003

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close