In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.