当前位置:网站首页> 详细聊聊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作用内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- MySQL lock common knowledge points & summary of interview questions
- LeetCode85+105+114+124
- ASP dynamically creates table table
- static关键字续、继承、重写、多态
- 在线文本数字识别列表求和工具
- MySQL backup database Linux
- math_ Basic elementary function graph (power function / exponent / logarithm / trigonometry / inverse trigonometry)
- With the rise of China's database, Alibaba cloud lifeifei: China's cloud database has taken the lead in various mainstream technological innovations abroad
- Is it appropriate to apply silicone paint to American Standard UL 790 class a?
- ASP using panel to realize simple registration page
猜你喜欢

VS2013如何让编写的程序在其它电脑上面也能运行

Simple understanding of why to rewrite hashcode and equals methods at the same time
![[multithreading] how to implement timer by yourself](/img/a9/dd9489c7a0028dd9d3a6dae9a71deb.png)
[multithreading] how to implement timer by yourself

5-1系统漏洞扫描

Huawei's software testing director with 7 years' experience, several suggestions for all students who want to switch to software testing

Underlying principles of file operations (file descriptors and buffers)

Optional类的高级使用

5-minute quick start pytest testing framework

5分钟快速上手 pytest 测试框架

解题元宇宙,网络游戏中的多元通信方案
随机推荐
软件测试方法和技术知识点有哪些?
云原生爱好者周刊:炫酷的 Grafana 监控面板集合
Cout ambiguous problem
MySQL lock common knowledge points & summary of interview questions
[proteus simulation] digital tube display of stepping motor speed
MySQL,MVCC详解,快照读在RC、RR下的区别
【多线程】 如何自己实现定时器
5-2Web应用程序漏洞扫描
从零实现深度学习框架——RNN从理论到实战【实战】
leetcode 416. Partition Equal Subset Sum 分割等和子集(中等)
LeetCode85+105+114+124
MooseFS 调优笔记
DevCloud加持下的青软,让教育“智”上云端
Grep tool
Mysql入库不了表情符号怎么办
math_基本初等函数图型(幂函数/指数/对数/三角/反三角)
Day9 ---- 用户注册与登录
jfinal中如何使用过滤器监控Druid监听SQL执行?
论文浅尝 | KR-GCN: 知识感知推理的可解释推荐系统
Qt5.14.2 error connecting to the MySQL database of Ubuntu 20.04