当前位置:网站首页>关于mysql的int型主键自增问题
关于mysql的int型主键自增问题
2022-06-22 02:51:00 【编程小龙】
引入
我们在使用mysql数据库时,习惯使用int型作为主键,并设置为自增,这既能够保证唯一,使用起来又很方便,但int型的长度是有限的,如果超过长度怎么办呢?
暴露问题
我们先创建一个测试表,创建语句如下:
CREATE TABLE test1 (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20)
)
然后我们插入两条数据:
INSERT INTO test1 VALUES(NULL,'小牛');
INSERT INTO test1 VALUES(NULL,'大牛');
查询表显示正常:
int型的有符号的范围为231 -1 = 2147483647,我们直接插入一条数据id为2147483647,如下:
INSERT INTO test1 VALUES(2147483647 ,'小华')
结果显示正常:
此时自增ID已达到了int型的上限,如果我再插入数据,就会报错:
INSERT INTO test1 VALUES(NULL,'母牛');

此时主键已无法自增,插入的id仍然是2147483647,就违反了主键唯一的条件,所以报错。
解决问题
(1)使用更大的数据类型bigint
bigint的范围是263-1,所谓指数爆炸,此时的大小达到了9,223,372,036,854,775,807的可怕量级,简单来说就是用bigint 一天100w条数据也得存200亿年才能自增爆炸,所以在当前场景,几乎不用担心bigint会自增满
我们修改数据类型为bigint,如图
再执行插入语句:
INSERT INTO test1 VALUES(NULL,'母牛');
又能够正常插入了:
(2)使用UUID作为主键
我们都知道,UUID会根据当前系统性能,时间戳等一系列参数经过运算得到一个全世界唯一的字符串,并且mysql提供了生成UUID的方法,用它作为主键能够保证数据的唯一性。
利用如下代码可以生成32位的UUID:
-- 生成32位UUID
SELECT REPLACE(UUID(),'-','') AS UUID;
然后咱们再创建一个测试表:
CREATE TABLE test2(
id VARCHAR(50) PRIMARY KEY,
NAME VARCHAR(20) NOT NULL
)
插入一条数据:
-- 插入UUID
INSERT INTO test2 VALUES(REPLACE(UUID(),'-',''),'老王');

但这样写插入语句每次都要手写UUID函数,貌似有点太麻烦了,咱们可以写一个触发器,让触发器自动为我们设置ID:
-- 创建触发器
DELIMITER $$
CREATE
TRIGGER auto_id -- 名称
BEFORE INSERT -- 触发时机
ON test2 FOR EACH ROW -- 作用于test2表,对每行数据生效
BEGIN
IF new.id = '' THEN -- 当id为空字符串时设置UUID
SET new.id = REPLACE(UUID(),'-','');
END IF;
END$$
插入一条数据:
-- 插入一条数据
INSERT INTO test2 VALUES('','小王');
结果能正常添加

总结
(1)用int型和bigInt型增删改查速度较UUID更快,并且更节省空间。
(2)用UUID更方便。
更详细的比较信息参考这篇博文:
MySQL 使用自增ID主键和UUID 作为主键的优劣比较详细过程(从百万到千万表记录测试)
边栏推荐
- June25,2022 PMP Exam clearance manual-3
- Using neo4j sandbox to learn neo4j graph data science GDS
- Sword finger offer 57 Next node of binary tree
- Latest release: neo4j figure data science GDS 2.0 and aurads GA
- Unity3d post process volume profile
- ATM机模拟系统
- How to use tensorboard add_ histogram
- Microblog closes publishing multiple part-time fraud information illegal accounts: how to crack down on data fraud
- In 2022, the number of mobile banking users in Q1 will reach 650million, and ESG personal financial product innovation will be strengthened
- fatal error: png++/png.hpp: 没有那个文件或目录
猜你喜欢

discuz! Bug in the VIP plug-in of the forum repair station help network: when the VIP member expires and the permanent member is re opened, the user group does not switch to the permanent member group

Neo4j 智能供应链应用源代码简析

xpm_ memory_ A complete example of using the tdpram primitive

JVM makes wheels

C++ primer Chapter 2 summary of variables and basic types

C ++ Primer 第2章 变量和基本类型 总结

华阳智能冲刺深交所:拟募资4亿 复星惟盈是股东

Two dot vertical progress styles

Game Jam开发周期

File upload vulnerability shooting range analysis upload_ LABS
随机推荐
Day14QProgressBar2021-10-17
Horizontal comparison of domestic API management platforms, which one is stronger?
Database daily question - day 19: top ranked travelers
Force buckle 141 Circular linked list
Relative references must start with either “/“, “./“, or “../“.
Game Jam开发周期
国产品牌OPPO官方最新出品!这份PPT报告!真刷新我对它认知了
Must the database primary key be self incremented? What scenarios do not suggest self augmentation?
Kubernetes code generator use
Which Amazon evaluation system is better?
【5. 高精度减法】
Write your own kubernetes controller
Official release of ideal L9: retail price of 459800 yuan will be delivered before the end of August
C3-qt realize Gobang games (I) November 7, 2021
【9. 子矩阵和】
Anaconda historical version download
Unity3d post process volume profile
Automated tools - monitoring file changes
[7. high precision division]
最热门海量的阿里云盘资源分享