Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove unique key from mysql table

I need to remove a unique key from my mysql table. How can remove that using mysql query.

I tried this but it is not working

alter table tbl_quiz_attempt_master drop unique key; 

Please help me

Thanks

like image 373
DEVOPS Avatar asked Feb 07 '12 06:02

DEVOPS


People also ask

How do I remove a unique key in SQL?

The syntax for dropping a unique constraint in MySQL is: ALTER TABLE table_name DROP INDEX constraint_name; table_name.

How do I drop a unique key in MySQL composite?

We can remove composite PRIMARY KEY constraint from multiple columns of an existing table by using DROP keyword along with ALTER TABLE statement.

How do I change the unique key in MySQL?

Sometimes we want to add a unique key to the column of an existing table; then, this statement is used to add the unique key for that column. Following are the syntax of the ALTER TABLE statement to add a unique key: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE(column_list);


2 Answers

All keys are named, you should use something like this -

ALTER TABLE tbl_quiz_attempt_master   DROP INDEX index_name; 

To drop primary key use this one -

ALTER TABLE tbl_quiz_attempt_master   DROP INDEX `PRIMARY`; 

ALTER TABLE Syntax.

like image 189
Devart Avatar answered Sep 22 '22 06:09

Devart


First you need to know the exact name of the INDEX (Unique key in this case) to delete or update it.
INDEX names are usually same as column names. In case of more than one INDEX applied on a column, MySQL automatically suffixes numbering to the column names to create unique INDEX names.

For example if 2 indexes are applied on a column named customer_id

  1. The first index will be named as customer_id itself.
  2. The second index will be names as customer_id_2 and so on.

To know the name of the index you want to delete or update

SHOW INDEX FROM <table_name> 

as suggested by @Amr

To delete an index

ALTER TABLE <table_name> DROP INDEX <index_name>; 
like image 20
Narendran Parivallal Avatar answered Sep 22 '22 06:09

Narendran Parivallal