从 MySQL 表中删除唯一约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3487691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:52:21  来源:igfitidea点击:

Dropping Unique constraint from MySQL table

mysql

提问by Ankur Mukherjee

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

如何使用 phpMyAdmin 在 MySQL 表的列上删除“唯一键约束”?

回答by Mark Byers

A unique constraint is also an index.

唯一约束也是索引。

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

首先使用SHOW INDEX FROM tbl_name找出索引的名称。索引的名称存储在该key_name查询结果中调用的列中。

Then you can use DROP INDEX:

然后你可以使用DROP INDEX

DROP INDEX index_name ON tbl_name

or the ALTER TABLEsyntax:

ALTER TABLE语法:

ALTER TABLE tbl_name DROP INDEX index_name

回答by systemovich

You can DROPa unique constraint from a table using phpMyAdmin as requestedas 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.

您可以根据要求使用 phpMyAdminDROP从表中获得唯一约束,如下表所示。对 Wingspan 字段施加了唯一约束。在这种情况下,约束的名称与字段名称相同。

alt text

替代文字

回答by thomasrutter

The indexes capable of placing a unique key constraint on a table are PRIMARYand UNIQUEindexes.

能够在表上放置唯一键约束的索引是PRIMARYUNIQUE索引。

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

要删除列上的唯一键约束但保留索引,您可以使用 type 删除并重新创建索引INDEX

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

请注意,所有表都标有索引是个好主意PRIMARY

回答by Umesh Patil

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

要使用 phpmyadmin 添加 UNIQUE 约束,请转到该表的结构并在下面找到并单击它,

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

要删除 UNIQUE 约束,请以同样的方式转到结构并向下滚动到“索引”选项卡,然后在下方找到并单击“下拉”, 在此处输入图片说明

Hope this works.

希望这有效。

Enjoy ;)

享受 ;)

回答by Jeffry Louis

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

对于 WAMP 3.0:单击添加 1 列下方的结构,您将看到“-索引”单击 -Indexes 并删除您想要的任何索引。

回答by Manjunatha H C

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

如果要从 mysql 数据库表中删除唯一约束,请使用带有删除索引的 alter table。

Example:

例子:

create table unique_constraints(unid int,activity_name varchar(100),CONSTRAINT activty_uqniue UNIQUE(activity_name),primary key (unid));

创建表 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_uqniueis UNIQUE constraint for activity_namecolumn.

其中activty_uqniueactivity_name列的唯一约束。

回答by Hamza.S

while dropping unique key we use index

在删除唯一键时,我们使用索引

ALTER TABLE tbl
DROP INDEX  unique_address;

回答by Lukasz Szozda

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;

更改表

从 MySQL 8.0.19 开始,ALTER TABLE 允许使用更通用(和 SQL 标准)的语法来删除和更改任何类型的现有约束,其中约束类型由约束名称确定: 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

db<>小提琴演示

回答by vignesh .B

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

我的表名是buyer,它有一个唯一的约束列emp_id 现在我要删除emp_id

step 1: exec sp_helpindex buyers, see the image file

第一步:exec sp_helpindex 买家,查看图片文件

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

步骤 3:更改表买家删除约束 [UQ__buyers__1299A860D9793F2E] 更改表买家删除列 emp_id

note:

笔记:

Blockquote

块引用

instead of buyers change it to your table name :)

而不是买家将其更改为您的表名:)

Blockquote

块引用

thats all column name emp_id with constraints is dropped!

这就是所有带有约束的列名 emp_id 都被删除了!

回答by ram

  1. First delete table

  2. go to SQL

  1. 先删除表

  2. 转到 SQL

Use this code:

使用此代码:

CREATE  TABLE service( --tablename 
  `serviceid` int(11) NOT NULL,--columns
  `customerid` varchar(20) DEFAULT NULL,--columns
  `dos` varchar(30) NOT NULL,--columns
  `productname` varchar(150) NOT NULL,--columns
  `modelnumber` bigint(12) NOT NULL,--columns
  `serialnumber` bigint(20) NOT NULL,--columns
  `serviceby` varchar(20) DEFAULT NULL--columns
)
--INSERT VALUES
INSERT INTO `service` (`serviceid`, `customerid`, `dos`, `productname`, `modelnumber`, `serialnumber`, `serviceby`) VALUES
(1, '1', '12/10/2018', 'mouse', 1234555, 234234324, '9999'),
(2, '09', '12/10/2018', 'vhbgj', 79746385, 18923984, '9999'),
(3, '23', '12/10/2018', 'mouse', 123455534, 11111123, '9999'),
(4, '23', '12/10/2018', 'mouse', 12345, 84848, '9999'),
(5, '546456', '12/10/2018', 'ughg', 772882, 457283, '9999'),
(6, '23', '12/10/2018', 'keyboard', 7878787878, 22222, '1'),
(7, '23', '12/10/2018', 'java', 11, 98908, '9999'),
(8, '128', '12/10/2018', 'mouse', 9912280626, 111111, '9999'),
(9, '23', '15/10/2018', 'hg', 29829354, 4564564646, '9999'),
(10, '12', '15/10/2018', '2', 5256, 888888, '9999');
--before droping table
ALTER TABLE `service`
  ADD PRIMARY KEY (`serviceid`),
  ADD  unique`modelnumber` (`modelnumber`),
  ADD  unique`serialnumber` (`serialnumber`),
  ADD unique`modelnumber_2` (`modelnumber`);
--after droping table
ALTER TABLE `service`
  ADD PRIMARY KEY (`serviceid`),
  ADD  modelnumber` (`modelnumber`),
  ADD  serialnumber` (`serialnumber`),
  ADD modelnumber_2` (`modelnumber`);