MySQL 如何从mysql表中删除唯一键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9172164/
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
How to remove unique key from mysql table
提问by DEVOPS
I need to remove a unique key from my mysql table. How can remove that using mysql query.
我需要从我的 mysql 表中删除一个唯一的键。如何使用 mysql 查询删除它。
I tried this but it is not working
我试过这个,但它不起作用
alter table tbl_quiz_attempt_master drop unique key;
Please help me
请帮我
Thanks
谢谢
回答by Devart
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`;
回答by Narendran Parivallal
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
首先,您需要知道 INDEX 的确切名称(在本例中为唯一键)以删除或更新它。
INDEX 名称通常与列名称相同。如果在一列上应用了多个 INDEX,MySQL 会自动为列名添加后缀以创建唯一的 INDEX 名称。
例如,如果在名为的列上应用 2 个索引customer_id
- The first index will be named as
customer_id
itself. - The second index will be names as
customer_id_2
and so on.
- 第一个索引将被命名为
customer_id
它自己。 - 第二个索引将是名称
customer_id_2
,依此类推。
To know the name of the index you want to delete or update
要知道要删除或更新的索引的名称
SHOW INDEX FROM <table_name>
as suggested by @Amr
正如@Amr所建议的
To delete an index
删除索引
ALTER TABLE <table_name> DROP INDEX <index_name>;
回答by Uday Sawant
ALTER TABLE mytable DROP INDEX key_Name;
回答by Amr
For those who don't know how to get index_name
which mentioned in Devart's answer, or key_name
which mentioned in Uday Sawant's answer, you can get it like this:
对于那些谁不知道如何让index_name
其中提到Devart的答案,或者key_name
其中提到乌代张以芳的回答,你可以这样说:
SHOW INDEX FROM table_name;
This will show all indexes for the given table, then you can pick name of the index or unique key that you want to remove.
这将显示给定表的所有索引,然后您可以选择要删除的索引或唯一键的名称。
回答by sam yi
Unique key is actually an index. http://codeghar.wordpress.com/2008/03/28/drop-unique-constraint-in-mysql/
唯一键实际上是一个索引。 http://codeghar.wordpress.com/2008/03/28/drop-unique-constraint-in-mysql/
回答by Sumit Kumar Gupta
There are two method two remove index in mysql. First method is GUI. In this method you have to open GUI interface of MYSQL and then go to that database and then go to that particular table in which you want to remove index.
在mysql中有两种方法二删除索引。第一种方法是 GUI。在这种方法中,您必须打开 MYSQL 的 GUI 界面,然后转到该数据库,然后转到要删除索引的特定表。
After that click on the structure option, Then you can see table structure and below you can see table indexes. You can remove indexes by clicking on drop option
之后点击结构选项,然后你可以看到表结构,下面你可以看到表索引。您可以通过单击删除选项来删除索引
Second method by
第二种方法通过
ALTER TABLE student_login_credentials DROP INDEX created_at;
here student_login_credentials is table name and created_at is column name
这里 student_login_credentials 是表名, created_at 是列名
回答by Adolf Han
To add a unique key use :
要添加唯一密钥,请使用:
alter table your_table add UNIQUE(target_column_name);
To remove a unique key use:
要删除唯一密钥,请使用:
alter table your_table drop INDEX target_column_name;