mysql表中unique约束名称的查看,添加以及删除

来源:互联网 发布:hello world程序员梗 编辑:程序博客网 时间:2024/05/22 02:28

场景:
在Mysql建立了1个unique约束,但是并没有给这个约束命名;
比如:在创建表过程中使用了下列语句website varchar(128) not null uniquewebsite字段设置为唯一的,当website字段并不需要唯一时该怎样删除这个unique约束呢?

show create table 表名; 可以查看表中建立的各种约束
示例:

show create table conferenceinfo;
执行后结果:
| conferenceinfo | CREATE TABLE conferenceinfo (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
cnname varchar(128) DEFAULT NULL,
enname varchar(128) DEFAULT NULL,
tag varchar(64) DEFAULT NULL,
location varchar(64) DEFAULT NULL,
sponsor varchar(64) DEFAULT NULL,
startdate date DEFAULT NULL,
enddate date DEFAULT NULL,
deadline date DEFAULT NULL,
acceptance date DEFAULT NULL,
website varchar(128) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY id (id,website),
KEY conference_tag_index (id,tag) USING BTREE,
KEY conference_startdate_index (startdate) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 |

示例中有一个唯一约束UNIQUE KEY id (id,website),名字为id,知道这个唯一约束的名字后就可以把这个约束进行删除


删除唯一约束

#alter table 表名 drop key 约束名;alter table conferenceinfo drop key id;

为表添加unique约束

格式:
alter table 表名 add unique key 约束名 (列1[,列2……])
alter table conferenceinfo add unique key cnname_website(cnname,website);

阅读全文
0 0
原创粉丝点击