PDA

View Full Version : Using email as Key column in Database.


woofy
02-16-2008, 05:05 AM
Hello, I dont know if this is a NO NO but I have accidentally discovered that if you use the members email address as a KEY column for a Database created in PHPMyAdmin that it wont allow any duplicate entries. This is one way of keeping usings registering multiple names with the same email. You kinda get an ugly duplicate message when you try to do it. This feature can also be used to keep multiple submissions from the same users, for example if you want to limit your users to voting only once on a voting page.

edbr
02-16-2008, 06:26 AM
you can make a colomn unique to avoid duplicate anyway

davidj
02-16-2008, 08:58 AM
never use input as a key. If there is a small chance that it could be duplicated then you cant use it

set an ID column to auto-increment within the db and use that

domedia
02-16-2008, 05:36 PM
dj, how would you get around duplicate email addresses in the DB?

I guess you could do a check for the email before you insert it.

davidj
02-16-2008, 07:26 PM
yes thats the only way really

you could also add an index to the email field in the db to speed things up when running a search to see if the email exists

edbr
02-18-2008, 12:46 AM
cant you set the e amil field as unique?

davidj
02-18-2008, 05:42 AM
yes but relying on input being unique is bad practice

by all means check for duplicates but dont rely on and use input for keys

edbr
02-18-2008, 08:53 AM
ok, good to know. can you explain why its bad practice is it unreliable then?

davidj
02-18-2008, 10:24 AM
your using a key which is relating to a unique record

if you rely on input values to determin the key there is a possability that it could be duplicated in that a typo could match a current address. Im not saying its against the law but i prefer to let the db handle keys as it does a better job than a person would. Thats not to say that you cant check for duplicates within columns.

I would also say it looks bad when the db throws an error which means its better to build a good error handling system which catches it before the db does