Code Ghar

Drop Unique Constraint in MySQL

Posted in script by hs on March 28, 2008

If you would like to drop a unique constraint from your table in MySQL, there is a very simple way to do it. Thanks to how to drop unique constraint, you can do so as well. The trick is to know that you have to drop an index, instead of a unique constraint. Consider the following example:

create table mytable (
accountNumber tinyint unsigned not null,
authCode tinyint unsigned not null,
authorized enum ('NO', 'YES') not null default 'NO',
constraint uk_mytable unique(accountNumber, authCode));

In this example, I am setting a name (of my choosing) for the constraint. The reason is if I want to do anything with the constraint, I have an easily recognizable name for it. Now if you want to drop a unique constraint, you do this (tested in MySQL 5.0 and confirmed it works):

alter table mytable drop index uk_mytable;

Remember, do not do the following (or any variation of it) because it will give you SQL error:

alter table mytable drop unique constraint uk_mytable;

To recap, if you want to drop a unique constraint in MySQL, you have to drop an index rather than the unique constraint itself. The reason, I guess, is that MySQL consider a unique constraint to be an index.

Tagged with: ,

3 Responses

Subscribe to comments with RSS.

  1. MySQL Drop Unique Constraint said, on November 18, 2008 at 2:07 am

    [...] Code Ghar provides a more thorough description. [...]

  2. mathengewachira said, on January 12, 2009 at 11:22 pm

    thanks for the tip

  3. Alexey said, on March 26, 2009 at 7:46 am

    CONSTRAINT may be a foreign key.
    in this case, to delete it use:
    alter table table drop foreign key fk_codec_config__codec_group;


Leave a Reply