场景:
在Mysql建立了1个unique
约束,但是并没有给这个约束命名;
比如:在创建表过程中使用了下列语句website varchar(128) not null unique
把website字段设置为唯一的,当website字段并不需要唯一时该怎样删除这个unique
约束呢?
show create table 表名; 可以查看表中建立的各种约束
示例:
show create table conferenceinfo;
执行后结果:
| conferenceinfo | CREATE TABLEconferenceinfo
(
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 KEYid
(id
,website
),
KEYconference_tag_index
(id
,tag
) USING BTREE,
KEYconference_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);