mysql自增
来源:SegmentFault
时间:2023-02-16 15:28:58 277浏览 收藏
来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习数据库相关编程知识。下面本篇文章就来带大家聊聊《mysql自增》,介绍一下MySQL,希望对大家的知识积累有所帮助,助力实战开发!
表结构
CREATE TABLE `salary` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL DEFAULT '0', `salary` decimal(10,2) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) )
auto_increment
# 查看当前表的auto_increment值 mysql> select auto_increment from information_schema.TABLES WHERE TABLE_SCHEMA='db_name' and TABLE_NAME='table_name'; # 修改当前auto_increment值 mysql> alter table db_name.table_name auto_increment=20
对auto_increment的理解
- 如果插入数据库时不指定auto_increment字段的值,那么该字段为auto_increment当前的值,然后 auto_increment自增1
- 如果插入数据库时指定了auto_increment字段的值,并且指定的值小于等于当前auto_increment的值(指定的值无重复),插入成功,但是auto_increment不会自增
- 如果插入数据库时指定了auto_increment字段的值,并且指定的值大于当前auto_increment的值,插入成功,auto_increment变为指定值加1
- 当前最大id大于auto_increment的值,插入时不指定auto_increment字段值,当自增id等于最大id时,会插入失败,然后auto_increment自增1
last_insert_id()
不带参数时,该函数返回最近一个insert语句生成的第一个自动增长列的值,值为bigint unsigned类型。
带参数时,返回一个unsigned int。
# 查看自增值 mysql> select last_insert_id(); -> 20
last_insert_id是几月每个服务连接的,即一个服务的insert语句不会影响到另一个服务的last_insert_id的值,虽然他们共享auto_increment。如下例
# 连接1 mysql server1> insert into salary (user_id,salary) value(4,32); mysql server1> select last_insert_id(); -> 5; mysql server1> select auto_increment from information_schema.TABLES WHERE TABLE_SCHEMA='test' and TABLE_NAME='salary'; -> 6; # 连接2 mysql server2> select last_insert_id(); -> 0; mysql server2> select auto_increment from information_schema.TABLES WHERE TABLE_SCHEMA='test' and TABLE_NAME='salary'; -> 6;
当插入的值中包含了自增列,那么last_insert_id不会更新,但是会更新auto_increment的值
mysql> select last_insert_id(); -> 5 mysql> insert into salary (id,user_id,salary) value(8,4,32); mysql> select last_insert_id(); -> 5 mysql> select auto_increment from information_schema.TABLES WHERE TABLE_SCHEMA='test' and TABLE_NAME='salary'; -> 9
一次插入多条数据,last_insert_id返回第一条成功插入后生成的id
mysql> select auto_increment from information_schema.TABLES WHERE TABLE_SCHEMA='test' and TABLE_NAME='salary'; -> 9 mysql> insert into salary (user_id,salary) values (4,32),(5,44); mysql> select last_insert_id(); -> 9 mysql> select auto_increment from information_schema.TABLES WHERE TABLE_SCHEMA='test' and TABLE_NAME='salary'; -> 11
last_insert_id(expr),指定一个表达式参数,该表达式的返回值会当做下一次调用last_insert_id的返回值。
mysql> update salary set id=last_insert_id(id+3) where id=5; mysql> select last_insert_id(); -> 8
参考链接
好了,本文到此结束,带大家了解了《mysql自增》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多数据库知识!
声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
最新阅读
更多>
-
285 收藏
-
259 收藏
-
374 收藏
-
475 收藏
-
483 收藏
-
462 收藏
-
469 收藏
-
289 收藏
-
239 收藏
-
315 收藏
-
361 收藏
-
184 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习