品易云推流 关闭
文章详情页
文章 > MySQL > mysql主键和唯一的区别

mysql主键和唯一的区别

mysql主键 唯一

头像

小妮浅浅

2021-08-25 09:25:465903浏览 · 0收藏 · 0评论

区别

1、一个表至多有一个主键,但可以有多个唯一。

2、主键不允许为空,唯一可以为空。

3、主键创建后一定包含一个唯一性索引。

唯一性索引并不一定就是主键。

4、唯一性索引列允许空值,而主键列不允许为空值。

实例

查看索引
show index from sdn_system_operation;
 
创建唯一索引
create unique index unique_index_operation_name on sdn_system_operation(operation_name);
create unique index unique_index_operation_code on sdn_system_operation(operation_code);
create unique index unique_index_operation_nameandcode on sdn_system_operation(operation_name,operation_code);
 
删除唯一索引
drop index unique_index_operation_name on sdn_system_operation;
drop index unique_index_operation_code on sdn_system_operation;
drop index unique_index_operation_nameandcode on sdn_system_operation;
 
 
 
将UNIQUE约束添加到已存在的表中
alter table sdn_system_operation constraint unique_index_operation_name unique(operation_name);
alter table sdn_system_operation constraint unique_index_operation_code unique(operation_code);
alter table sdn_system_operation constraint unique_index_operation_nameandcode unique(operation_name,operation_code);
 
解除唯一索引
alter table sdn_system_operation drop index unique_index_operation_name;
alter table sdn_system_operation drop index unique_index_operation_code;
alter table sdn_system_operation drop index unique_index_operation_nameandcode;

以上就是mysql主键和唯一的区别,希望对大家有所帮助。更多mysql学习指路:MySQL

推荐操作系统:windows7系统、mysql5.8、DELL G3电脑

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图