mysql主键id的生成方式(自增、唯一不规则)

目录
  • 1、利用uuid函数生成唯一且不规则的主键id
  • 2、id实现自动增长

1、利用uuid函数生成唯一且不规则的主键id

sql:

create table `test` (
  `id` varchar(100) collate utf8_estonian_ci not null comment '唯一不重复',
  `create_time` timestamp not null default current_timestamp,
  `update_time` timestamp not null default current_timestamp on update current_timestamp,
  `sex` int(11) default null,
  `name` varchar(255) collate utf8_estonian_ci default null,
  `username` varchar(255) collate utf8_estonian_ci default null,
  `password` varchar(255) collate utf8_estonian_ci default null,
  `classes` varchar(255) collate utf8_estonian_ci default null,
  `major` int(255) default null,
  `qq` int(20) default null,
  `introducemyself` varchar(255) collate utf8_estonian_ci default null,
  primary key (`id`)
) engine=innodb default charset=utf8 collate=utf8_estonian_ci row_format=dynamic;

表:

插入语句:

insert into test(id,sex,name,username,password,classes,major,qq,introducemyself) value(replace(uuid(), '-', ''),1,"小米","xck","001","八班",265,953190259,"我最牛");

执行了俩次,生成了俩个不同id:

2、id实现自动增长

将类型改为整型,下方选择自动增长

 看ddl:

create table `test` (
  `id` bigint(100) not null auto_increment comment '唯一不重复',
  `create_time` timestamp not null default current_timestamp,
  `update_time` timestamp not null default current_timestamp on update current_timestamp,
  `sex` int(11) default null,
  `name` varchar(255) collate utf8_estonian_ci default null,
  `username` varchar(255) collate utf8_estonian_ci default null,
  `password` varchar(255) collate utf8_estonian_ci default null,
  `classes` varchar(255) collate utf8_estonian_ci default null,
  `major` int(255) default null,
  `qq` int(20) default null,
  `introducemyself` varchar(255) collate utf8_estonian_ci default null,
  primary key (`id`)
) engine=innodb default charset=utf8 collate=utf8_estonian_ci row_format=dynamic;

插入一条数据,sql:

insert into test(sex,name,username,password,classes,major,qq,introducemyself) value(1,"小米","xck","001","八班",265,953190259,"我最牛");

对应数据库变化:

到此这篇关于mysql主键id的生成方式(自增、唯一不规则)的文章就介绍到这了,更多相关mysql主键id生成内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐