Wednesday, May 20, 2009

MySQL:Set up unique key ignoring duplicates

For many of you who might be having trouble setting up a unique key on
a table containing duplicates, here's the key

mysql> ALTER IGNORE TABLE table_name ADD UNIQUE(column_name)

Please note the keyword IGNORE in the above statement. That does the
work forcefully.

4 comments:

chorny said...

"If IGNORE is specified, only the first row is used of rows with duplicates on a unique key, The other conflicting rows are deleted. Incorrect values are truncated to the closest matching acceptable value." - don't use this if you want duplicates to remain.

Ketan said...

Yes, that's true. But if your application is in the development mode, you can save time with IGNORE. Is there any better way? If you know, please post here :-)

Thank you Chorny!

Ketan

indavao said...
This post has been removed by a blog administrator.
tagskie said...

hi.. just dropping by here... have a nice day! http://kantahanan.blogspot.com/