当前位置:网站首页>[What is the role of auto_increment in MySQL?】
[What is the role of auto_increment in MySQL?】
2022-07-31 08:29:00 【m0_67402564】
Directory
Source of problem
Many times, the word [auto_increment] will appear in MySQL statements. Most of the time, tables are automatically generated. You will learn it when you first learn the MySQL database. Later, you may forget it gradually.What does effect mean?Here's a summary:
Explanation
auto_increment is used for primary key auto-increment
, it starts to grow from 1, when you delete the first record and insert the second data, the primary key value is 2, not 1.
Example:
create table test
(
id
int(10) not null auto_increment, – means auto-increment
name
varchar(20) not null,
primary key(id
)
)
auto_increment = 1; - Indicates the auto-increment starting size-- This way you can create a table test
, the id is the auto-increment column
– execute the statement insert into test (name
) values ('name');
– you can insert a row of data as: 1 'name'
Notes
When using AUTO_INCREMENT, the following points should be noted:
1
, AUTO_INCREMENT is an attribute of the data column, only applicable to the integer type
data column.
2
, the data column for setting the AUTO_INCREMENT attribute should be a positive sequence
, so the data column should be declared as UNSIGNED, so that the number of the sequence can be increased by onetimes.
3
, the AUTO_INCREMENT data column must have a unique index
to avoid repeated serial numbers (that is, the primary key or part of the primary key).
AUTO_INCREMENT data column must have NOT NULL
attribute.
4
, the maximum value of the serial number of the AUTO_INCREMENT data column is constrained by the data type of the column. For example, the maximum number of the TINYINT data column is 127, and if UNSIGNED is added, the maximum value is 255.Once the upper limit is reached, AUTO_INCREMENT will fail.
5
. When a full table is deleted, MySQL AUTO_INCREMENT will start numbering from 1 again.
This is because when performing a full table operation, MySQL (the best combination with PHP) actually does this optimization operation
: first delete all data and indexes in the data table,Then rebuild the data table.
If you want to delete all data rows and keep the sequence number information, you can use a delete command with where to suppress the optimization of MySQL (the best combination with PHP): delete from table_name where 1;
Use last_insert_id() to get the just incremented value.
Description: Part of the content comes from the search and sorting, the purpose is to record the summary and learn to use~
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- A, MySQL principle of master-slave replication
- 【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)-- 组件UI
- 【小程序专栏】总结uniapp开发小程序的开发规范
- SQL 嵌套 N 层太长太难写怎么办?
- 《c语言小游戏》入门级三子棋游戏(机器人加强版)
- Vulkan与OpenGL对比——Vulkan的全新渲染架构
- First acquaintance with NK-RTU980 development board
- 分布式缓存系统必须要解决的四大问题
- 使用MySQL如何查询一年中每月的记录数
- SQL连接表(内连接、左连接、右连接、交叉连接、全外连接)
猜你喜欢
随机推荐
实用生物信息学2:多组学数据整合和挖掘
MySql database optimization query tool
《c语言小游戏》入门级三子棋游戏(机器人加强版)
SQL语句知识大全
UML图及在drawio中的绘制
MySQL 5.7详细下载安装配置教程
《opencv学习笔记》-- 仿射变换
使用PageHelper实现分页查询(详细)
关于yum源的配置及更新
【云原生】微服务之Feign的介绍与使用
MySQL 5.7升级到8.0详细过程
Read Elephant Swap in one article, why does it bring such a high premium to ePLATO?
MySQL table creation statement_Three commonly used MySQL table creation statements
一、MySQL主从复制原理
Shell编程之条件语句
sqli-labs(less-11)
Client navicat installation tutorial
如何升级nodejs版本
Unreal基础概念
Linux redis6.2.6 configuration file