本篇内容主要讲解“mysql数据库主键id是怎样生成的”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mysql数据库主键id是怎样生成的”吧!
1、利用uuid函数生成唯一且不规则的主键id
2、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:
将类型改为整型,下方选择自动增长
看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是怎样生成的”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。