找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 mysql教程 查看内容

如何在MySQL中更改自动递增的起始数字?

作者:模板之家 2023-8-21 19:09 101人关注

Theauto_incrementisadefaultpropertythatautomaticallyincrementsthenewlyaddedrecordby1.Theauto_incrementcanbechangedfromthebeginningaswell.Theprocedureforthatisgivenbelowminus;First,atableiscreated.mysq ...

如何在MySQL中更改自动递增的起始数字?

The auto_increment is a default property that automatically increments the newly added record by 1. The auto_increment can be changed from the beginning as well. The procedure for that is given below −

First, a table is created.

mysql> CREATE table DemoAuto
-> (
-> id int auto_increment,
-> name varchar(100),
-> primary key(id)
-> );
Query OK, 0 rows affected (0.47 sec)
登录后复制

After that the alter table command is used to change the starting number of auto_incremnt which starts from 1 by default. The starting value is changed to 100.

mysql> alter table DemoAuto auto_increment = 100;
Query OK, 0 rows affected (0.24 sec)
Records: 0 Duplicates: 0 Warnings: 0
登录后复制

Then some records are inserted into the table.This is given as follows −

mysql> INSERT into DemoAuto(name) values('John');
Query OK, 1 row affected (0.10 sec)

mysql> INSERT into DemoAuto(name) values('Smith');
Query OK, 1 row affected (0.13 sec)

mysql> INSERT into DemoAuto(name) values('Bob');
Query OK, 1 row affected (0.22 sec)
登录后复制

The select statement is used to obtain the table values as output. This is given below −

mysql> SELECT * from DemoAuto;
登录后复制

The following is the output obtained −

+-----+-------+
| id  | name  |
+-----+-------+
| 100 | John  |
| 101 | Smith |
| 102 | Bob   |
+-----+-------+
3 rows in set (0.00 sec)
登录后复制

In the above output, the record id starts from 100.


路过

雷人

握手

鲜花

鸡蛋
原作者: 网络收集 来自: 网络收集

全部回复(0)