Get the App
SLTechnology News&Howtos  ›  Database  › 

How to realize the automatic growth of Primary key in mysql

Shulou Source: shulou.com Published: 2022-05-31 18:58:29 10月02日 Update

This article shows you how to achieve automatic primary key growth in mysql, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Let's build a table user first.

Mysql > create table user (

-> id int (4) primary key auto_increment

-> username varchar (20)

-> password varchar (20))

Query OK, 0 rows affected (0.08 sec)

2. Let's insert a piece of data first:

Mysql > insert into user values (null,'admin',300000)

Query OK, 1 row affected (0.09 sec)

3. The primary key value we inserted is null, but the insertion is successful. View the values of the table:

Mysql > select * from user

+-- +

| | id | username | password | |

+-- +

| | 1 | admin | 300000 | |

+-- +

1 row in set (0.00 sec)

The id in the table is still 1.

4. Let's insert another piece of normal data:

Mysql > insert into user (username,password) values ('Europa','123')

Query OK, 1 row affected (0.03 sec)

5, after automatic growth is 2, what about in the database? Let's take a look:

Mysql > select * from user

+-- +

| | id | username | password | |

+-- +

| | 1 | admin | 300000 | |

| | 2 | Europa | 123 | |

+-- +

2 rows in set (0.00 sec)

The value in the database is 2

6. Let's insert another piece of data that specifies the primary key value manually:

Mysql > insert into user values (4 recordings europathetic pencils 123')

Query OK, 1 row affected (0.24 sec)

7. If you press automatic growth, it will be 3. Let's look at the values in the database:

Mysql > select * from user

+-- +

| | id | username | password | |

+-- +

| | 1 | admin | 300000 | |

| | 2 | Europa | 123 | |

| | 4 | europa | 123 | |

+-- +

3 rows in set (0.00 sec)

It's not the value we guessed, it's the manually set value of 4.

You can continue to experiment, but you will eventually find that if you manually set the primary key value to null, the database will ignore null and automatically increase the value; if you manually give other legal values

It will not automatically grow and give the value according to your given value.

Therefore, it can be concluded that when the primary key in mysql grows automatically, he will first check whether the inserted primary key value is legal, and if it is not legal, it will report an error; furthermore, check whether the inserted primary key value is empty, if so, then

Auto-grow assignment; furthermore, if you don't set a value for the primary key when you insert the data, you will also automatically increase the value.

The above is how to achieve automatic primary key growth in mysql. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Growth data manual database legal content that is skills knowledge simplicity success concise articles more articles conclusions industry information information channels or Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology Shulou Tech Info MariaDB Linux