Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop unique constraint from mysql table on a foreign key column

I want to drop just the UNIQUE Constraint from my MySQL table column and keep the Foreign Key Constraint on the column as-is. work_id is the foreign key. Initially, the column was supposed to be unique (one-to-one relationship) which is now not needed. I'm using MySQL Ver 15.1 Distrib 5.5.64-MariaDB.

DESCRIBE requests;
+---------------------+---------------------------------------+------+-----+---------+-------+
| Field               | Type                                  | Null | Key | Default | Extra |
+---------------------+---------------------------------------+------+-----+---------+-------+
| request_id          | char(32)                              | NO   | PRI | NULL    |       |
| owner               | varchar(100)                          | NO   |     | NULL    |       |
| status              | enum('PENDING','ACCEPTED','REJECTED') | YES  |     | NULL    |       |
| work_id             | char(32)                              | NO   | UNI | NULL    |       |
| response_message    | varchar(3000)                         | YES  |     | NULL    |       |
| created_date        | datetime                              | NO   |     | NULL    |       |
| last_modified_date  | datetime                              | NO   |     | NULL    |       |
+---------------------+---------------------------------------+------+-----+---------+-------+

CREATE TABLE `requests` (   
`request_id` char(32) NOT NULL,   
`owner` varchar(100) NOT NULL,   
`status` enum('PENDING','ACCEPTED','REJECTED') DEFAULT NULL,   
`work_id` char(32) NOT NULL,   
`response_message` varchar(3000) DEFAULT NULL,   
`created_date` datetime NOT NULL,   
`last_modified_date` datetime NOT NULL,   
PRIMARY KEY (`request_id`),   
UNIQUE KEY `work_id` (`work_id`),   
CONSTRAINT `requests_ibfk_1` FOREIGN KEY (`work_id`) REFERENCES `work` (`work_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1

I want to remove UNIQUE Constraint from the work_id. I did some search and executed the following commands.

SHOW INDEX FROM requests;
+-----------------+------------+----------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table           | Non_unique | Key_name       | Seq_in_index | Column_name       | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------------+------------+----------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| requests        |          0 | PRIMARY        |            1 | request_id        | A         |          16 |     NULL | NULL   |      | BTREE      |         |               |
| requests        |          0 | work_id        |            1 | work_id           | A         |          16 |     NULL | NULL   |      | BTREE      |         |               |
+-----------------+------------+----------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

And then executed

ALTER TABLE requests DROP INDEX work_id;

I get an error

ERROR 1553 (HY000): Cannot drop index 'work_id': needed in a foreign key constraint

like image 461
Java Learner Avatar asked Oct 22 '25 22:10

Java Learner


2 Answers

So, your problem is you are trying to drop a index which is used in Foreign Key Constraint. So you can not do it directly. Follow below steps:

  1. Drop the constraint requests_ibfk_1 which is your foreign key.
alter table requests drop foreign key requests_ibfk_1
  1. Then Drop the UNIQUE KEY on column work_id.
alter table requests drop index work_id
  1. Again Add Foreign Key on Column work_id.
alter table requests add CONSTRAINT `requests_ibfk_1` FOREIGN KEY (`work_id`) REFERENCES `work` (`work_id`)

DEMO

like image 137
Akhilesh Mishra Avatar answered Oct 25 '25 11:10

Akhilesh Mishra


The problem is that the definition of the unique constraint drops the index which is normally created for the foreign key. But there is another way, without recreating the foreign key constraint or (temporarily) disabling the checks (which can lead to consistency errors).

First, add another index for the same column (for logical reasons, I would name it exactly as the foreign key):

CREATE INDEX requests_ibfk_1 ON requests(work_id);

Now you can safely drop the unique constraint/index (since there is still an index available for the foreign key constraint):

DROP INDEX work_id ON requests;

I hope this solves the problem.

like image 39
Phillip Freitter Avatar answered Oct 25 '25 13:10

Phillip Freitter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!