简体   繁体   English

如何从外键列上的 mysql 表中删除唯一约束

[英]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.我只想从我的 MySQL 表列中删除UNIQUE Constraint ,并按原样保留列上的Foreign Key Constraint work_id is the foreign key. work_id是外键。 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.我正在使用 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 .我想从 work_id 中删除UNIQUE Constraint 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错误 1553 (HY000):无法删除索引“work_id”:在外键约束中需要

So, your problem is you are trying to drop a index which is used in Foreign Key Constraint.所以,你的问题是你试图删除一个Foreign Key约束中使用的索引。 So you can not do it directly.所以你不能直接这样做。 Follow below steps:按照以下步骤操作:

  1. Drop the constraint requests_ibfk_1 which is your foreign key .删除作为foreign key的约束requests_ibfk_1
alter table requests drop foreign key requests_ibfk_1
  1. Then Drop the UNIQUE KEY on column work_id .然后将UNIQUE KEY放在列work_id上。
alter table requests drop index work_id
  1. Again Add Foreign Key on Column work_id .再次在列work_id上添加Foreign Key
alter table requests add CONSTRAINT `requests_ibfk_1` FOREIGN KEY (`work_id`) REFERENCES `work` (`work_id`)

DEMO 演示

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.我希望这能解决问题。

As described in @Rick James's comment, the alternative is to temporarily disable key constraints of a table, immediately drop the unique index, and then enable the keys of the table afterwards.正如@Rick James 的评论中所述,另一种方法是暂时禁用表的键约束,立即删除唯一索引,然后再启用表的键。 Here is an example for mysql / mariadb :这是mysql / mariadb的示例:

ALTER TABLE `<YOUR_TABLE_NAME>` DISABLE KEYS;
ALTER TABLE `<YOUR_TABLE_NAME>` DROP INDEX `<YOUR_UNIQUE_INDEX_NAME>`;
ALTER TABLE `<YOUR_TABLE_NAME>` ENABLE KEYS;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM