245

How can I drop the "Unique Key Constraint" on a column of a MySQL table using phpMyAdmin?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Ankur Mukherjee
  • 3,777
  • 5
  • 32
  • 39

10 Answers10

422

A unique constraint is also an index.

First use SHOW INDEX FROM tbl_name to find out the name of the index. The name of the index is stored in the column called key_name in the results of that query.

Then you can use DROP INDEX:

DROP INDEX index_name ON tbl_name

or the ALTER TABLE syntax:

ALTER TABLE tbl_name DROP INDEX index_name
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 1
    Sorry sir i had tried it before raising the question ,but its not working – Ankur Mukherjee Aug 15 '10 at 14:26
  • @Ankur Mukherjee: I forgot to mention: You have to change tbl_name and index_name to the name of your actual table and the name of the actual index, respectively. You can see the names in MySQL Query Browser. You can also do `SHOW CREATE TABLE tbl_name`. – Mark Byers Aug 15 '10 at 14:33
  • Error Message:"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(player_id,year,tournament)' at line 1" – Ankur Mukherjee Aug 15 '10 at 14:56
  • @Ankur Mukherjee: That's because you're not using the name of the index. The name of the index is defined when you create the index. Find out the name of the index and use the index name in the DROP INDEX statement. – Mark Byers Aug 15 '10 at 14:59
  • You can also use `SHOW INDEX FROM tbl_name` to show information about all indexes on a table, including the name of the index. – Mark Byers Aug 15 '10 at 15:00
  • INDEX name refers to the name of the column on which "UNIQUE KEY" constraint is specified isn't it sir? – Ankur Mukherjee Aug 15 '10 at 15:01
  • @Ankur Mukherjee: No, that is not correct. The index name might or might not be the same as the column name. Usually it is not. – Mark Byers Aug 15 '10 at 15:03
  • so there is no way of just removing the unique constraint of a column?(other than removing index) – Ankur Mukherjee Aug 15 '10 at 15:04
  • 1
    @Ankur Mukherjee: The way I have suggested is in my opinion the best way to do it. – Mark Byers Aug 15 '10 at 15:33
  • 1
    I read somewhere in the question **using phpMyAdmin**... The answer provided by @systemovich should be marked as the accepted one. – Pere Sep 16 '14 at 14:16
  • To delete you can use DROP INDEX Key_name ON tbl_name – SteveTz Jun 05 '15 at 15:15
  • ```SHOW INDEX FROM tbl_name``` use it to see all index then drop it – vahiiiid Jul 24 '18 at 07:20
  • @MarkByers Hi Sir, Do you have a suggestion for reading on DDL constraints and unique keys? Its killing my interview-readyness – chrips Aug 08 '19 at 19:27
131

You can DROP a unique constraint from a table using phpMyAdmin as requested as shown in the table below. A unique constraint has been placed on the Wingspan field. The name of the constraint is the same as the field name, in this instance.

alt text

Geoffrey
  • 5,407
  • 10
  • 43
  • 78
  • 35
    It is important to note that the indexes section is collapsed by default, and is expanded by a small, subtle link in 12px font where the section is in the screenshot. But thanks to you, I found it. Bless your face. +1 – Jacklynn May 25 '13 at 17:39
11

The indexes capable of placing a unique key constraint on a table are PRIMARY and UNIQUE indexes.

To remove the unique key constraint on a column but keep the index, you could remove and recreate the index with type INDEX.

Note that it is a good idea for all tables to have an index marked PRIMARY.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167
  • 4
    Upvoted because @thomasrutter has touched on the fact that removing a unique constraint **may adversely affect query performance** - and this can be mitigated by replacing the `UNIQUE` index with a regular `INDEX`. – Alex Apr 18 '14 at 16:17
9

To add UNIQUE constraint using phpmyadmin, go to the structure of that table and find below and click that,

enter image description here

To remove the UNIQUE constraint, same way, go to the structure and scroll down till Indexes Tab and find below and click drop, enter image description here

Hope this works.

Enjoy ;)

Umesh Patil
  • 4,668
  • 32
  • 24
3

If you want to remove unique constraints from MySQL database table, use alter table with drop index.

Example:

CREATE TABLE unique_constraints (
    unid INT,
    activity_name VARCHAR(100),
    CONSTRAINT activty_uqniue UNIQUE (activity_name),
    PRIMARY KEY (unid)
);
ALTER TABLE unique_constraints
DROP INDEX activty_uqniue;

Where activty_uqniue is UNIQUE constraint for activity_name column.

Random Person
  • 163
  • 1
  • 17
Manjunatha H C
  • 217
  • 2
  • 3
2

For WAMP 3.0 : Click Structure Below Add 1 Column you will see '- Indexes' Click -Indexes and drop whichever index you want.

1

The constraint could be removed with syntax:

ALTER TABLE

As of MySQL 8.0.19, ALTER TABLE permits more general (and SQL standard) syntax for dropping and altering existing constraints of any type, where the constraint type is determined from the constraint name: ALTER TABLE tbl_name DROP CONSTRAINT symbol;

Example:

CREATE TABLE tab(id INT, CONSTRAINT unq_tab_id UNIQUE(id));

-- checking constraint name if autogenerated
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'tab';

-- dropping constraint
ALTER TABLE tab DROP CONSTRAINT unq_tab_id;

db<>fiddle demo

Community
  • 1
  • 1
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
1

This might help:

Inside your sql terminal

FIRST STEP:

SHOW INDEX FROM {YOUR_TABLE_NAME}

SECOND STEP:

SHOW INDEX FROM {YOUR_TABLE_NAME} WHERE Column_name='ACTUAL_COLUMN_NAME_YOU_GOT_FROM_FIRST_STEP_OUTPUT'

THIRD STEP:

ORIGINAL_KEY_NAME_VALUE = SECOND_STEP_RESPONSE["Key_name"]

FOURTH STEP:

ALTER TABLE {YOUR_TABLE_NAME} DROP INDEX ${ORIGINAL_KEY_NAME_VALUE}

Asad Zaman
  • 475
  • 5
  • 10
-1

while dropping unique key we use index

ALTER TABLE tbl
DROP INDEX  unique_address;
Hamza.S
  • 1,319
  • 9
  • 18
-3

my table name is buyers which has a unique constraint column emp_id now iam going to drop the emp_id

step 1: exec sp_helpindex buyers, see the image file

step 2: copy the index address

enter image description here

step3: alter table buyers drop constraint [UQ__buyers__1299A860D9793F2E] alter table buyers drop column emp_id

note:

Blockquote

instead of buyers change it to your table name :)

Blockquote

thats all column name emp_id with constraints is dropped!

s__
  • 9,270
  • 3
  • 27
  • 45