aspose file tools*
The moose likes JDBC and the fly likes How to make the MySQL db to avoid duplicate entry? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to make the MySQL db to avoid duplicate entry?" Watch "How to make the MySQL db to avoid duplicate entry?" New topic
Author

How to make the MySQL db to avoid duplicate entry?

Namitha karupaiya
Ranch Hand

Joined: Mar 10, 2004
Posts: 89
Is there any way to make the MySQL database aumatically reject duplicate entry in a column other than Primary Key column?

Thank you very much.
Renato Losio
Ranch Hand

Joined: Nov 23, 2005
Posts: 99
Yes, using unique constrain.

Make a unique column so you get no dupplicates.

alter table yourTable add unique yourColumn;

You may have one primary key but many unique columns...

Cheers,

Renato


Renato Losio - www.arsenio.it - renatoweb@arsenio.it
Namitha karupaiya
Ranch Hand

Joined: Mar 10, 2004
Posts: 89
Thank you very much Renato Losio !

My requirement is to store email addresses in a column and what you have stated seems a great idea.

Now, say, for example the following email address is already in the database column:
someone@example.com

Will the unique column resist the following entry:
SOMEONE@EXAMPLE.COM

If not are there any feature built in the database to achieve this?

Thank you very much.
Renato Losio
Ranch Hand

Joined: Nov 23, 2005
Posts: 99
Generally speaking, no.

But you may always store the email as lowercase (or uppercase) and the unique constraint will work.

I mean if you have the column email, change the:

email = 'test@email.com'

to something like:

email = upper('test@email.com')

Cheers,

Renato
 
 
subject: How to make the MySQL db to avoid duplicate entry?