How to restore mysql growth from 1

Source: Internet
Author: User

How to restore mysql growth from 1

How to restore the problem of automatic mysql growth from 1
In a table, I set autoid to auto-increment column.
For example, the following data is available:

1 Zhang Weinan 20
2 Wang wumale 22
3 Li Si male 25
4. Chen damang 19

Now I delete the two records of autoid = 3 and autoid = 4.
Then insert a data record, for example:

Insert into userinfo (autoid, username, sex, age) values ('null', 'chunge', 'male', '27 ');

However, there is a problem: the number starts from 5, not 3. After all, my third and fourth records have been deleted.

Could you tell me how to start autoid from 3.



------ Solution --------------------
Alter table t_Myxiao7 AUTO_INCREMENT 3;

SQL code
Mysql> create table t_Myxiao7 (id int not null auto_increment primary key, name varchar (10), genda varchar (10), age int); Query OK, 0 rows affected (0.08 sec) mysql> insert into t_Myxiao7 values-> (null, 'zhang san', 'male', 20),-> (null, 'wang wu', 'male', 22 ), -> (null, 'Li si', 'male', 25),-> (null, 'chen da', 'male', 19); Query OK, 4 rows affected (0.05 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from t_Myxiao7; + ---- + ------ + ------- + ------ + | id | name | genda | age | + ---- + ------ + ------- + ------ + | 1 | Zhang San | male | 20 | 2 | Wang Wu | male | 22 | 3 | Li Si | male | 25 | 4 | Chen da | male | 19 | + ---- + ------ + ------- + ------ + 4 rows in set (0.01 sec) mysql> delete from t_Myxiao7 where id = 3 or id = 4; Query OK, 2 rows affected (0.08 sec) mysql> select * from t_Myxiao7; + ---- + ------ + ------- + ------ + | id | name | genda | age | + ---- + ------ + ------- + ------ + | 1 | Zhang San | male | 20 | 2 | Wang Wu | male | 22 | + ---- + ------ + ------- + ------ + 2 rows in set (0.00 sec) mysql> alter table t_Myxiao7 AUTO_INCREMENT 3; Query OK, 2 rows affected (0.19 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> insert into t_Myxiao7 values-> (null, 'chunge', 'male', '27'); Query OK, 1 row affected (0.06 sec) mysql> select * from t_Myxiao7; + ---- + ------ + ------- + ------ + | id | name | genda | age | + ---- + ------ + ------- + ------ + | 1 | Zhang San | male | 20 | 2 | Wang Wu | male | 22 | 3 | Chun Ge | male | 27 | + ---- + ------ + ------- + ------ + 3 rows in set (0.00 sec) mysql>


ITOKIT. COM prompt:If the data in the table is useless. If you delete the data directly, the automatic growth ID will not start from 1, you can use "clear data table ". In this way, IDs are automatically increased from 1.

 

This article Reprinted from: http://www.myexception.cn/mysql/827353.html

 


How does mysql automatically increase the number of primary keys from 1 to 1?

Auto_increment
Create table test (
Id int primary key auto_increment,
....
)
---------------- And above -------------
Modify the table and add auto_increment:
Alter table test modify id int primary key auto_increment;

In mysql, how does one increase the number of databases from 1?

The truncate statement clears the table content, including the information of the auto-incrementing primary key. After the truncate table is created, the table's primary key starts from 1 again. Syntax: truncate table table1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.