How to create an INT field (not a primary key) that begins at 1000 with auto_increment in MySQL?


For this, you need to set AUTO_INCREMENT as 1000 −

alter table yourTableName AUTO_INCREMENT = 1000;

Let us first create a table −

mysql> create table DemoTable639 (
   StudentId int PRIMARY KEY,
   StudentStartId int AUTO_INCREMENT,
   StudentName VARCHAR(50),
   INDEX(StudentStartId)
);
Query OK, 0 rows affected (0.86 sec)

Following is the query to set auto increment with value 1000 −

mysql> alter table DemoTable639 AUTO_INCREMENT = 1000;
Query OK, 0 rows affected (0.28 sec)
Records: 0 Duplicates: 0 Warnings: 0

Insert some records in the table using insert command −

mysql> insert into DemoTable639(StudentId,StudentName) values(1,'John');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable639(StudentId,StudentName) values(2,'Chris');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable639(StudentId,StudentName) values(3,'Robert');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable639(StudentId,StudentName) values(4,'David');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable639;

This will produce the following output −

+-----------+----------------+-------------+
| StudentId | StudentStartId | StudentName |
+-----------+----------------+-------------+
|         1 |           1000 | John        |
|         2 |           1001 | Chris       |
|         3 |           1002 | Robert      |
|         4 |           1003 | David       |
+-----------+----------------+-------------+
4 rows in set (0.00 sec)

Updated on: 23-Aug-2019

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements