当前位置:网站首页>[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
边栏推荐
- 正则表达式绕过
- @Transactional注解的失效场景
- sqli-labs(less-11)
- 使用PageHelper实现分页查询(详细)
- MySQL安装常见报错处理大全
- Linked list implementation and task scheduling
- 7/28-7/29 Expectation + thinking + suffix array + ST table
- 关于@Autowired
- "C language game" entry-level chess game (robot enhanced version)
- Open Source | Commodity Recognition Recommender System
猜你喜欢
随机推荐
SSM框架简单介绍
《c语言》青蛙跳台阶递归问题
个人报错问题 持续总结
TypeError The view function did not return a valid response. The function either returned None 的解决
【小程序专栏】总结uniapp开发小程序的开发规范
蚂蚁核心科技产品亮相数字中国建设峰会 持续助力企业数字化转型
循环结构--for循环
安装部署KubeSphere管理kubernetes
正则表达式绕过
XSS详解
MySQL中InnoDB的多版本并发控制(MVCC)的实现
[PSQL] Complex query
Ubuntu安装Mysql5.7
MySQL 5.7 安装教程(全步骤、保姆级教程)
分布式缓存系统必须要解决的四大问题
Vscode: Project-tree plugin
【云原生&微服务五】Ribbon负载均衡策略之随机ThreadLocalRandom
【Objective-C语言中的@property】
ZCMU--1862: zbj的狼人杀
SQL 嵌套 N 层太长太难写怎么办?