这篇文章主要为大家展示了“mysql主键和唯一的区别有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“mysql主键和唯一的区别有哪些”这篇文章吧。
区别
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主键和唯一的区别有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注天达云行业资讯频道!