Originally posted by Purushothaman Thambu:
One simple thing you can do is declare email as primary key. You don't have to fire the query to check if email already exists or not.
This is only a good idea is you know for certain that a user's email address will
never change. Far safer is a surrogate key.
But i want this should happen in a single query.
Can't be done in a single query. Well, you could wrap the two steps up in a Stored Procedure, but then all you are doing is hiding that it has two steps. You could define a unique index on email and report the constraint violation to the user. However, this is using Exceptions as conditionals which is not usually considered a good thing to do. Your two query idea is probably the best way to handle this. If your email address has to be unique, I'd declare the constraint too.