[thelist] Free Tip about SQL

rudy rudy937 at rogers.com
Fri Jul 18 18:15:25 CDT 2003

> No, two entries in the users table can not have the same
> users name. How do I prevent this from happening?

declare a unique constraint on the username

> Well I don't know if it's the best way to do it but when
> I receive the data I first query the database to see
> if a record with the username given is already in there.

with the unique constraint in place, skip the query and just do the insert

the database will tell you if it rejected a dupe

> Of course if there is a record found the user is told so
> and told to do something about it.

with a unique constraint, you would "trap" the database error and give the
user the same nicely formatted friendly error message

let's say that out of every 100 attempted entries, you end up rejecting 5
because they would be dupes

your way -- query, followed by insert -- you would do 195 database calls

my way, it's exactly 100

approximately twice as efficient, eh



More information about the thelist mailing list