

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Autoincrement in MySQL begins from 1? How can we begin it from another number?
The autoincrement in MySQL gives a unique number every time. By default, it starts at 1. If you want to start from another number, then you need to change the auto-increment value with the help of ALTER command or you can give value at the time of table creation.
Let us first create a table −
mysql> create table DemoTable ( UniqueNumber int NOT NULL AUTO_INCREMENT, PRIMARY KEY(UniqueNumber) ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------------+ | UniqueNumber | +--------------+ | 1 | | 2 | | 3 | | 4 | +--------------+ 4 rows in set (0.00 sec)
Following is the query to change the auto_increment value to begin from another number −
mysql> alter table DemoTable AUTO_INCREMENT=1000; Query OK, 0 rows affected (0.18 sec) Records: 0 Duplicates: 0 Warnings: 0
Insert some records in the table using insert command −
mysql> insert into DemoTable values(); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output. Now the new auto_increment value inserted and begins from 1000 −
+--------------+ | UniqueNumber | +--------------+ | 1 | | 2 | | 3 | | 4 | | 1000 | | 1001 | | 1002 | +--------------+ 7 rows in set (0.00 sec)
- Related Questions & Answers
- How can I set my auto-increment value to begin from 1 in MySQL?
- How to Reset MySQL AutoIncrement using a MAX value from another table?
- In MySQL, how can we convert a value from one number system to the value in another number system?
- How can we copy one array from another in Java
- Can we implement one interface from another in java?
- Is it necessary to select the database each time we begin a MySQL session? How can it be done?
- Can we add a column to a table from another table in MySQL?
- How we can copy Python modules from one system to another?
- Display auto increment user id sequence number to begin from 001 in MySQL?
- What does it mean by select 1 from MySQL table?
- How can we revoke privileges from a MySQL user?
- How can we use MySQL SELECT without FROM clause?
- How can we remove a column from MySQL table?
- How do I begin auto increment from a specific point in MySQL?
- How can we call one constructor from another in the same class in C#?