Drop Unique Constraint in MySQL
March 28, 2008 12 Comments
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.
Pingback: MySQL Drop Unique Constraint
thanks for the tip
CONSTRAINT may be a foreign key.
in this case, to delete it use:
alter table table drop foreign key fk_codec_config__codec_group;
Pingback: MySQL Drop Unique Constraint
Thank for your help!
To add unique key
ALTER TABLE table_Name ADD UNIQUE(col1,col2,..);
To Drop Unique key
Unique key is dropped as INDEX
First see the name of unique key with show create table table_Name
Let say unique key name is uniqueKeyName. Normally it is col1 i.e first column name of unique key
ALTER TABLE table_Name DROP INDEX uniqueKeyName;
Thank you
it was really helpful .thanks a lot
Thanks for help
thanks! solved a big problem!!!
Thanks
what about dropping an unique constraint which has not been given any name, e.g. the unique constraint which has been applied this way:
ALTER TABLE names ADD COLUMN email VARCHAR(255) UNIQUE;
I have tried all the ways to delete the unique constraint on email column, but nothing working.