mysql 加主键、uniqkey

加主键:
mysql> alter table sbtest1 add primary key PK_testNoPK (id);

加唯一键约束:
alter table employees add constraint idxunique_first_name_gender unique(first_name, gender);
mysql> ALTER TABLE tb_dept1
-> ADD CONSTRAINT unique_name UNIQUE(name);

删除唯一约束:
删除数据表 tb_dept1 中的唯一约束 unique_name,SQL 语句和运行结果如下所示。
mysql> ALTER TABLE tb_dept1
-> DROP INDEX unique_name;

查看约束:
select distinct CONSTRAINT_NAME
from information_schema.TABLE_CONSTRAINTS
where table_name = 'table_name' and constraint_type = 'UNIQUE';

mysql> show create table sbtest1\G
*************************** 1. row ***************************
Table: sbtest1
Create Table: CREATE TABLE sbtest1 (
id int(11) NOT NULL AUTO_INCREMENT,
k int(11) NOT NULL DEFAULT '0',
c char(120) NOT NULL DEFAULT '',
pad char(60) NOT NULL DEFAULT '',
PRIMARY KEY (id),
UNIQUE KEY idxunique_first_name_gender (id,k)
) ENGINE=InnoDB AUTO_INCREMENT=682735 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

你可能感兴趣的:(mysql 加主键、uniqkey)