I have a database that needs to have unique values wherein it will provide an error saying "Username already exist" and will not add to database. How to do this?
AngelicOne
4
Posting Whiz
Recommended Answers
Jump to PostYou can select the user name from the database to see if it is already used:
SELECT COUNT(*) FROM user_table WHERE userid = 'WhateverTheUserTypedIn'
If this returns anything other than zero, that userid is already in use.In some databases you could make the userid field an identity field, …
Jump to PostAlternatively, you can use IF ELSE in queries with SQL Server 2000 or newer.
You can check if the item exists, if it doesn't then insert it. I have done something like this in the past:IF EXISTS ( SELECT * FROM user_table WHERE userid = @userID )
BEGIN
…
Jump to PostPsuedo code:
Get the username
Use the select from above to get the count of the username
if count == 0 then
add user to the database
else
display "user name already exists"
endif
All 9 Replies
Momerath
1,327
Nearly a Senior Poster
Featured Poster
AngelicOne
commented:
thanks
+1
Geekitygeek
480
Nearly a Posting Virtuoso
PierlucSS
commented:
good one.
+1
AngelicOne
4
Posting Whiz
Momerath
1,327
Nearly a Senior Poster
Featured Poster
AngelicOne
4
Posting Whiz
Momerath
1,327
Nearly a Senior Poster
Featured Poster
AngelicOne
4
Posting Whiz
Lusiphur
185
Practically a Posting Shark
Team Colleague
Featured Poster
AngelicOne
4
Posting Whiz
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.