当前位置:网站首页>[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
边栏推荐
猜你喜欢
随机推荐
shell/bash脚本命令教程
【Objective-C语言中的@property】
Open Source | Commodity Recognition Recommender System
35-Jenkins-共享库应用
"C language game" entry-level chess game (robot enhanced version)
Docker-compose安装mysql
正则表达式绕过
力扣 593. 有效的正方形
Collation and sharing of related classic papers and datasets in the field of deep learning communication
"C language" frog jumping steps recursion problem
功能强大的国产Api管理工具
如何在 Linux 上安装 MySQL
Install the deployment kubernetes KubeSphere management
开源|商品识别推荐系统
google搜索技巧——程序员推荐
MySQL 5.7详细下载安装配置教程
动态顺序表的增删查改(C语言实现)
New in Visual Studio: Low Priority Builds
7/28-7/29 Expectation + thinking + suffix array + ST table
如何在一台机器上(windows)安装两个MYSQL数据库