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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:57:04+00:00 2026-05-31T15:57:04+00:00

In my server, which is connected to postgresql, should I check if the username

  • 0

In my server, which is connected to postgresql, should I check if the username already exists in the table by doing "select * ..." and then getting the number of rows in the resultset and i the number of rows is equal to zero, then insert the username?
Or just insert the username in the table. If it already exists, then it will throw an error which can then be caught.

Note:The username is the primary key

Doing which of the above two is better?

  • 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-05-31T15:57:05+00:00Added an answer on May 31, 2026 at 3:57 pm

    You should do the “try-and-catch exception” method simply because you have to do it anyway.

    If you check first, there’s nothing to stop someone inserting a row for that user between your check and your insert, in which case the user will be in the table even though your check didn’t find it.

    Short of being able to run the check-and-insert within some sort of transaction (so that nobody else can insert that user in the interim). you can’t be certain that the non-exception will work.

    And although many DBMS’ provide transactional support, I don’t know of any that will lock a row you have yet to insert 🙂

    Of course, if your application is designed in such a way that only your process will be inserting users (and serialised), you can use the check-first method. But I’d be putting in copious comments to the effect that it will need to be revisited if ever you scale up.

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

Sidebar

Related Questions

I have an S: which is connected via a username that exists both on
How can I tell if my Postgresql server is running or not? I'm getting
Im connected to a chat server which gives me messages i print out in
I want to create server which is able to be connected with multiple clients.
If I've been told a table (or proc) name, but not which connected database
Assume I have a server application which is working in computer not connected to
Essentially I want a basic Java Server which multiple people can be connected to
I am creating one text file which will connected to some server. this text
I have a server with PostgreSQL 8.4 which is being rebooted every night at
I have a WCF service which is connected to an sql server database which

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.