当前位置:网站首页>[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'

insert image description here

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

原网站

版权声明
本文为[m0_67402564]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/212/202207310742438692.html