• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unique datatype in database

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a database in which I have make the userName field unique. So that only one userName exist in the database.
Now if user enters his information then How I can check that if the username already exists in the database?
Should I have to do:
and then compare the user Names with each other?
Or is there any other way to do this.
Thanks in advance...
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, your query would be

and you would test to see if that query returned any records or not.

Don't be tempted to use string concatenation to create that query, though. Use a PreparedStatement and plug in the user ID via a parameter.
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, if you want to avoid case-insensitive differences, you might consider "lower(userID) = lower(?)". Granted, some databases can be configured to always perform case insensitive matching.

One issue with unique usernames is that even if you check ahead of time, a race condition could have two users creating the same record at the same time. It doesn't mean you shouldn't check ahead of time, you should always avoid throwing exceptions if they can be solved through application logic, but you should always have a pathway that handles when the database throws a uniqueness violation even after checking ahead of time.
 
What's gotten into you? Could it be this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic