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 8015203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:11:17+00:00 2026-06-04T20:11:17+00:00

I have a MySQL table which has a field for email addresses which is

  • 0

I have a MySQL table which has a field for email addresses which is defined as unique. For this example, let’s say that all my form does is allow a user to insert their email address into the table.

Since the email field is unique, the query should fail should they try to enter the same email twice. I’m curious about the trade-offs between between the two scenarios:

1) Run a quick SELECT statement before performing the insert. If the select returns results, inform the user, and do not run the INSERT statement.

2) Run the INSERT statement, and check for a duplicate entry error

// snippet uses PDO
if (!$prep->execute($values))
{
    $err = $prep->errorInfo();
    if (isset($err[1]))
    {
        // 1062 - Duplicate entry
        if ($err[1] == 1062)
            echo 'This email already exists.';
    }
}

Also, please assume normal use, meaning that duplicate entries should be minimal. Therefore, in the first scenario you obviously have the overhead of running an additional query for every insert, whereas in the second you’re relying on error handling.

Also, I’m curious to hear thoughts on coding style. My heart says ‘Be a defensive programmer! Check the data before you insert!’ while my brain says ‘Hmmm, maybe it’s better to let MySQL take care of checking the data for you’.

EDIT – Please note this isn’t a “How do I do this” question, but rather a “Why should I do this a particular way” question. The little code snippet I included works, but what I’m curious about is the best way to solve the problem.

  • 1 1 Answer
  • 0 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-06-04T20:11:19+00:00Added an answer on June 4, 2026 at 8:11 pm

    INSERT + check status should be a better approach. With SELECT + INSERT you can have another thread insert the same value between the SELECT and the INSERT, which means you would need to also wrap those two statements in a table lock.

    It is easy to err on the side of too much defense in your coding. Python has the saying that “it is easier to ask for forgiveness than to ask for permission”, and this philosophy is not really Python-specific.

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

Sidebar

Related Questions

I have a table in MySQL 4.0 which currently has a year field as
Let's say I have a very large MySQL table with a timestamp field. So
I have a mySQL table which has a field where I keep my arrays
I have a MySql table which has about 100k rows. there is one field
I have a MySQL database table that has a VARCHAR field storing a date
I have a certain table in mySQL which has a field called image with
i have a leads table that has a field named referrer..which has data like
My ISPs server uses MySQL. I have a table which has a field with
I have a huge MySQL table which has its rows encoded in UTF-8 twice.
if i have a table which has columns with fixed lenght, Will mySQL count

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.