Mysql中如何建唯一性约束表

Mysql中如何建唯一性约束表

1. 单列唯一性约束
a. 直接建表
CREATE TABLE t1 (
id INT NOT NULL ,
uname VARCHAR( 20 ) NOT NULL ,
PRIMARY KEY ( id ) ,
UNIQUE (uname)) ENGINE = MYISAM ;

b. 删除唯一性约束
ALTER TABLE t1 DROP INDEX uname

c. 添加唯一性约束
ALTER TABLE t1 ADD UNIQUE (uname)

2. 2列联合唯一性约束
a. 直接建表
CREATE TABLE t1 (
id int(11) NOT NULL AUTO_INCREMENT,
user int(11) NOT NULL,
subacount varchar(40) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY user (user,subacount)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

b. 删除联合唯一性约束
ALTER TABLE t1 DROP INDEX user

c. 添加联合唯一性约束
ALTER TABLE t1 ADD UNIQUE user (user,subacount)

要发表评论,您必须先登录