当前位置:网站首页> 详细聊聊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作用内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- Spark cluster installation
- Can the flick CDC be used for incremental synchronization from Oracle to MySQL
- MySQL 锁常见知识点&面试题总结
- Hidden worries behind the listing of shushulang: the performance has declined significantly, the market position is relatively backward, and the competitiveness is questionable
- Code sharing for making and developing small programs on the dating platform
- MySQL backup and restore
- 正如以往我们对于互联网的看法一样,我们对于互联网的认识开始变得深度
- Day9 ---- 用户注册与登录
- Daily question brushing record (VIII)
- Underlying principles of file operations (file descriptors and buffers)
猜你喜欢

This time, I will talk about technology and life

MySQL,MVCC详解,快照读在RC、RR下的区别

If you master these 28 charts, you will no longer be afraid to be asked about TCP knowledge during the interview

【无工具搭建PHP8+oracle11g+Windows环境】内网/无网络/Win10/PHP连接oracle数据库实例

ASP using panel to realize simple registration page

解题元宇宙,网络游戏中的多元通信方案

math_基本初等函数图型(幂函数/指数/对数/三角/反三角)

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

Simple understanding of why to rewrite hashcode and equals methods at the same time

With the rise of China's database, Alibaba cloud lifeifei: China's cloud database has taken the lead in various mainstream technological innovations abroad
随机推荐
The child component of a single data flow modifies the value of the parent component
Detailed description of gaussdb (DWS) complex and diverse resource load management methods
便携式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
AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs
英语没学好到底能不能做coder,别再纠结了先学起来
合宙AIR32F103CBT6开发板上手报告
交友平台小程序制作开发代码分享
ASP using panel to realize simple registration page
Day9 ---- 用户注册与登录
LeetCode85+105+114+124
Does rapid software delivery really need to be at the cost of security?
leetcode:91. 解码方法【dfs + 记忆化】
客户端可以连接远程mysql
Realizing deep learning framework from zero -- LSTM from theory to practice [theory]
2022年第一季度保险服务数字化跟踪分析
5-2web application vulnerability scanning
Kubernetes architecture that novices must know
Unicom warehousing | all Unicom companies that need to sell their products need to enter the general warehouse first
Dynamics 365online lookup lookup field multiple selection