Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

UNIQUE constraint vs checking before INSERT

$
0
0

I have a SQL server table RealEstate with columns - Id, Property, Property_Value. This table has about 5-10 million rows and can increase even more in the future. I want to insert a row only if a combination of Id, Property, Property_Value does not exist in this table.

Example Table -

1,Rooms,51,Bath,21,Address,New York2,Rooms,22,Bath,12,Address,Miami

Inserting 2,Address,Miami should NOT be allowed. But, 2,Price,2billion is okay. I am curious to know which is the "best" way to do this andwhy. The why part is most important to me.

  1. Check if a row exists before you insert it.
  2. Set unique constraints on all 3 columns and let the database do the checking for you.

Is there any scenario where one would be better than the other ?

Thanks.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>