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.

5 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

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

It is useful to try everything in practise anyway and I like that here it's always possible to find something new. :)

Anonymous said...

Nice dispatch and this enter helped me alot in my college assignement. Thank you on your information.