当前位置:网站首页> 详细聊聊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作用内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- 低代码、端到端,一小时构建IoT示例场景,声网发布灵隼物联网云平台
- A mysql IBD file is too large processing record
- MySQL lock common knowledge points & summary of interview questions
- One click file sharing software jirafeau
- 【无工具搭建PHP8+oracle11g+Windows环境】内网/无网络/Win10/PHP连接oracle数据库实例
- The MySQL data cannot be read after the checkpoint recovery. Do you know the reason
- 合宙AIR32F103CBT6开发板上手报告
- 关于深度学习的概念理解(笔记)
- Huawei's software testing director with 7 years' experience, several suggestions for all students who want to switch to software testing
- 稳!上千微服务接入 Zadig 的最佳姿势(Helm Chart 篇)
猜你喜欢

The soft youth under the blessing of devcloud makes education "smart" in the cloud

AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践

Steady! The best posture for thousands of microservices to access Zadig (helm chart)

The logic behind the three whys encountered in technical communication

2022年第一季度保险服务数字化跟踪分析

为什么要同时重写hashcode和equals方法之简单理解

Mysql入库不了表情符号怎么办

Code sharing for making and developing small programs on the dating platform

Hidden worries behind the listing of shushulang: the performance has declined significantly, the market position is relatively backward, and the competitiveness is questionable

What are the software testing methods and technical knowledge points?
随机推荐
生产环境AIX小机报错B6267342问题处理
Mysql入库不了表情符号怎么办
在线文本数字识别列表求和工具
With the rise of China's database, Alibaba cloud lifeifei: China's cloud database has taken the lead in various mainstream technological innovations abroad
Arrange the array into the smallest number_ Reverse pairs in an array (merge Statistics)_ Number of occurrences of a number in an ascending array_ Ugly number (Sword finger offer)
MySQL lock common knowledge points & summary of interview questions
2022年第一季度保险服务数字化跟踪分析
Unicom warehousing | all Unicom companies that need to sell their products need to enter the general warehouse first
泰山OFFICE技术讲座:一行中所有元素高度相同
联通入库|需要各地联通公司销售其产品的都需要先入总库
解题元宇宙,网络游戏中的多元通信方案
Realizing deep learning framework from zero -- LSTM from theory to practice [theory]
Dynamics 365online lookup lookup field multiple selection
Can you be a coder if you don't learn English well? Stop worrying and learn it first
qt5.14.2连接ubuntu20.04的mysql数据库出错
PhpSpreadsheet读写Excel文件
Underlying principles of file operations (file descriptors and buffers)
One click file sharing software jirafeau
Cout ambiguous problem
正如以往我们对于互联网的看法一样,我们对于互联网的认识开始变得深度