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

时间:2022-08-27 12:37:21

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

sql:

  1. CREATE TABLE `test` (
  2. `id` varchar(100) COLLATE utf8_estonian_ci NOT NULL COMMENT '唯一不重复',
  3. `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  4. `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  5. `sex` int(11) DEFAULT NULL,
  6. `name` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  7. `username` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  8. `password` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  9. `classes` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  10. `major` int(255) DEFAULT NULL,
  11. `QQ` int(20) DEFAULT NULL,
  12. `introducemyself` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  13. PRIMARY KEY (`id`)
  14. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_estonian_ci ROW_FORMAT=DYNAMIC;

表:

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

插入语句:

  1. 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:

  1. CREATE TABLE `test` (
  2. `id` bigint(100) NOT NULL AUTO_INCREMENT COMMENT '唯一不重复',
  3. `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  4. `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  5. `sex` int(11) DEFAULT NULL,
  6. `name` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  7. `username` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  8. `password` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  9. `classes` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  10. `major` int(255) DEFAULT NULL,
  11. `QQ` int(20) DEFAULT NULL,
  12. `introducemyself` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  13. PRIMARY KEY (`id`)
  14. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_estonian_ci ROW_FORMAT=DYNAMIC;

插入一条数据,sql:

  1. INSERT INTO test(sex,name,username,password,classes,major,QQ,introducemyself) VALUE(1,"小米","xck","001","八班",265,953190259,"我最牛");

对应数据库变化:

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

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

原文链接:https://blog.csdn.net/weixin_41954142/article/details/91379829