場景:
在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);