欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-12 13:45:19
目录1、利用uuid函数生成唯一且不规则的主键idsql:create table `test` ( `id` varchar(100) collate utf8_estonian_ci not n...

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;

表:

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

插入语句:

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

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

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

2、id实现自动增长

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

mysql主键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的生成方式(自增、唯一不规则)的文章就介绍到这了,更多相关mysql主键id生成内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!