Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 690085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:23:58+00:00 2026-05-14T02:23:58+00:00

I have a database table that has a Unique Key constraint defined to avoid

  • 0

I have a database table that has a Unique Key constraint defined to avoid duplicate records from occurring.

I’m curious if it is bad practice to NOT manually check for duplicate records prior to running an INSERT statement on the table.

In other words, should I run a SELECT statement using a WHERE clause that checks for duplicate values of the record that I am about to INSERT. If a record is found, then do not run the INSERT statement, otherwise go ahead and run the INSERT….

OR

Just run the INSERT statement and try/catch the exception that may be thrown due to a Unique Key violation.

I’m weighing the two perspectives and can’t decide which is best-
1. Don’t waste a SELECT call to check for duplicates when I can just trap for an exception VS
2. Don’t be lazy by implementing ugly try/catch logic
VS
3. ???Your thoughts here??? 🙂

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:23:58+00:00Added an answer on May 14, 2026 at 2:23 am

    You really have to use the try..catch method. It may be less elegant, but it’s foolproof.

    If there is more than one client updating this table, then another client may insert a record between your check and your insert. You can still check if you want to, to save trying to do the insert needlessly. It might be a small performance increase if that’s what you’re worried about, but only if there is a duplicate. But every time there isn’t a duplicate, you’ve paid a performance penalty by doing both a SELECT and an INSERT.

    Anyway, what are the odds that there will be a constraint violation? Probably small, so why bother with the check?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an SQL table that has a unique key (or other) constraint
I have a table within my database that has many records, some records share
I have an SQL Server 2008 database with a table that has a column
I have a database named contacts.db with a table named TABLE_NAME that has one
I have a database that has a table email_eml that stores 3 attributes name_eml,
I am designing a database for a project. I have a table that has
I have a database that has node & nodetype tables. Nodes table NodeID ParentNodeID
I have a SQL database that has a table with a field set to
I have a SQL database that has the following table: Table: PhoneRecords -------------- ID(identity
I have an application that has a status table see Database best practices -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.