当前位置:网站首页> 详细聊聊MySQL中auto_increment有什么作用
详细聊聊MySQL中auto_increment有什么作用
2022-06-29 22:16:00 【1024问】
问题来源
解释
注意事项
总结
问题来源很多时候,MySQL语句中会出现【auto_increment】这个词汇,大多数时候,表都是自动生成的,刚开始学习MySQL数据库时会学习到,后来,渐渐地可能会忘记,那么这个语句的作用到底是什么意思?下面,来汇总一下:
解释auto_increment是用于主键自动增长的,从1开始增长,当你把第一条记录删除时,再插入第二条数据时,主键值是2,不是1。
例如:
create table test(id int(10) not null auto_increment, – 表示自增列name varchar(20) not null,primary key(id))
auto_increment = 1; – 表示自增起始大小-- 这样就可以创建一个表test,id为自增列
– 执行语句 insert into test (name) values (‘名字’);
– 就可以插入一行数据为: 1 ‘名字’
在使用AUTO_INCREMENT时,应注意以下几点:
1、AUTO_INCREMENT是数据列的一种属性,只适用于整数类型数据列。
2、设置AUTO_INCREMENT属性的数据列应该是一个正数序列,所以应该把该数据列声明为UNSIGNED,这样序列的编号个可增加一倍。
3、AUTO_INCREMENT数据列必须有唯一索引,以避免序号重复(即是主键或者主键的一部分)。
AUTO_INCREMENT数据列必须具备NOT NULL属性。
4、AUTO_INCREMENT数据列序号的最大值受该列的数据类型约束,如TINYINT数据列的最大编号是127,如加上UNSIGNED,则最大为255。一旦达到上限,AUTO_INCREMENT就会失效。
5、当进行全表删除时,MySQL AUTO_INCREMENT会从1重新开始编号。
这是因为进行全表操作时,MySQL(和PHP搭配之最佳组合)实际是做了这样的优化操作:先把数据表里的所有数据和索引删除,然后重建数据表。
如果想删除所有的数据行又想保留序列编号信息,可这样用一个带where的delete命令以抑制MySQL(和PHP搭配之最佳组合)的优化:delete from table_name where 1;
可用last_insert_id()获取刚刚自增过的值。
说明:部分内容来自搜索整理,目的是为了记录总结,学习使用~
总结到此这篇关于MySQL中auto_increment有什么作用的文章就介绍到这了,更多相关MySQL中auto_increment作用内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- Just like our previous views on the Internet, our understanding of the Internet began to become deeper
- 生产环境AIX小机报错B6267342问题处理
- 便携式4K音视频会议终端一体机带8倍数字变焦
- Why does copying files on a shared folder on a local area network (ERP server) result in the loss of the local Internet
- 正如以往我们对于互联网的看法一样,我们对于互联网的认识开始变得深度
- Day9 - user registration and login
- 直播平台开发,进入可视区域执行动画、动效、添加样式类名
- Problem solving metauniverse, multi communication scheme in online games
- 读书郎上市背后隐忧:业绩下滑明显,市场地位较靠后,竞争力存疑
- 文件操作的底层原理(文件描述符与缓冲区)
猜你喜欢
Hezhou air32f103cbt6 development board hands-on Report
夏日彩虹来下饭
[multithreading] how to implement timer by yourself
Why does copying files on a shared folder on a local area network (ERP server) result in the loss of the local Internet
Vs2013 how to make the program run on other computers
Mysql入库不了表情符号怎么办
DevCloud加持下的青软,让教育“智”上云端
Code sharing for making and developing small programs on the dating platform
中国数据库崛起,阿里云李飞飞:中国云数据库多种主流技术创新已领先国外
math_基本初等函数图型(幂函数/指数/对数/三角/反三角)
随机推荐
leetcode:91. Decoding method [DFS + memorization]
泰山OFFICE技术讲座:一行中所有元素高度相同
leetcode:91. 解码方法【dfs + 记忆化】
Unicom warehousing | all Unicom companies that need to sell their products need to enter the general warehouse first
5 - 1 Analyse de vulnérabilité du système
Guangzhou launched a campaign to promote the safety of bottled gas and popularized the knowledge of gas safety
在线文本数字识别列表求和工具
从检查点恢复后读取不到mysql的数据有那位兄台知道原因吗
Qt5.14.2 error connecting to the MySQL database of Ubuntu 20.04
mysql备份数据库linux
软件快速交付真的需要以安全为代价吗?
Steady! The best posture for thousands of microservices to access Zadig (helm chart)
便携式4K音视频会议终端一体机带8倍数字变焦
PhpSpreadsheet读写Excel文件
【Proteus仿真】步进电机转速数码管显示
The soft youth under the blessing of devcloud makes education "smart" in the cloud
Realizing deep learning framework from zero -- LSTM from theory to practice [theory]
Advanced use of the optional class
26岁,0基础转行软件测试,从月薪3k到16k,我整理的超全学习指南
Matplotlib histogram