MySQL笔记-唯一键的使用

在建表过程中,使用ID作为唯一标识。一般采用数字串,比如从1到xxx,或者20200215xxxx等。

如果要用户名要唯一就需要使用唯一键了。

唯一但不需要是主键,就使用唯一键

 

如下

create table test1(
id int primary key auto_increment,
username varchar(16) unique
)charset utf8;

create table test2(
id int primary key auto_increment,
username varchar(16),
unique key(username)
)charset utf8;

create table test3(
id int primary key auto_increment,
username varchar(16)
)charset utf8;

alter table test3 add unique key(username);

查看下:

上面是命令行,只要稍微看得懂就行了。下面是用工具操作:

工具还是简单啊

发布了1269 篇原创文章 · 获赞 1970 · 访问量 179万+

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/104335267