MySQL数据库之MySQL重置自增长值
小标 2018-12-21 来源 : 阅读 1448 评论 0

摘要:本文主要向大家介绍了MySQL数据库之MySQL重置自增长值 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

本文主要向大家介绍了MySQL数据库之MySQL重置自增长值 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

Summary: in this tutorial, we’ll  show you various ways to reset auto increment values of AUTO_INCREMENT columns in MySQL.

MySQL provides you with a useful feature called auto-increment. You can assign the auto-increment attribute to a column of a table to generate unique identity for the new row. Typically you use the auto-increment attribute for the primary key column of a table.

Whenever you insert a new row into a table, MySQL automatically assigns a sequence number to the auto-increment column. For example, if the table has 8 rows and you insert a new row without specifying the value for the auto-increment column, MySQL will automatically inserts a new row with id value 9.

Sometimes, you may need to reset the value of the auto-increment column so that the first record’s identity that you insert into the table starts from a specific number e.g., 1.

In MySQL you can reset auto increment values in various ways.

MySQL reset auto increment value examples

First, let’s create a tmp table and assign AUTO_INCREMENT attribute to id primary key column.

CREATE TABLE tmp (   id int(11) NOT NULL AUTO_INCREMENT,   name varchar(45) DEFAULT NULL,   PRIMARY KEY (id) );

Second, insert some sample data into the tmp table:

INSERT INTO tmp(name) VALUES('test 1'), ('test 2'), ('test 3');

Third, query the tmp table to see if the insert operations has been done successfully:

SELECT * FROM tmp;


We have three rows with values of ID column are 1, 2, and 3. Perfect! It is time to practice reset auto increment value of the ID column.

MySQL reset auto increment value by using ALTER TABLE statement

You can reset auto increment value by using the ALTER TABLE statement. The syntax of the ALTER TABLE statement to reset auto increment value is as follows:

ALTER TABLE table_name AUTO_INCREMENT = value;

You specify the table name after the ALTER TABLE clause and the value which we want to reset to in the expression AUTO_INCREMENT = value.

Notice that the value must be greater than or equal to the current maximum value of the auto-increment column.

Let’s delete the last record in the tmp table with id value 3:

DELETE FROM tmp WHERE ID = 3;

If you insert a new row, MySQL will assign 4 to id column of for new row. However, you can reset the number generated by MySQL to 3  by using the ALTER TABLE statement as the following:

ALTER TABLE tmp AUTO_INCREMENT = 3;

Now, let’s try to insert a new row into the tmp table  and query data from it to see the effect:

INSERT INTO tmp(name) VALUES ('MySQL example 3'); SELECT * FROM tmp;


We have three rows with the last auto-increment value is 3 instead of 4, which is what we expected.

MySQL reset auto increment value by using TRUNCATE TABLE statement

The TRUNCATE TABLE statement removes all the data of a table and reset auto-increment value to zero. The following illustrates the syntax of the TRUNCATE TABLE statement:

TRUNCATE TABLE table_name;

By using the TRUNCATE TABLE statement, you can only reset the auto-increment value to zero. In addition, all the data in the table is wiped out so you should use the TRUNCATE TABLE statement with extra caution.

MySQL reset auto increment value using DROP  TABLE and CREATE TABLE statements.

You can use a pair of statements: DROP TABLE and CREATE TABLE to reset the auto-increment column. Like the TRUNCATE TABLE statement, those statements removes all the data and reset the auto-increment value to zero.

DROP TABLE table_name; CREATE TABLE table_name(...);

In this tutorial, you have learned how to reset auto-increment value in MySQL in various ways. The first way is preferable because it is the easiest way and has no side effect.

本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程