How to make a primary key start from 1000?

MySQLMySQLi Database

To make a primary key start from 1000, you need to alter your table and set to auto_increment with value 1000. The syntax is as follows −

alter table yourTableName auto_increment=1000;

To understand the above syntax, let us first create a table. The query to create a table is as follows −

mysql> create table PrimaryKey1000Demo
   -> (
   -> ProductId int auto_increment,
   -> PRIMARY KEY(ProductId)
   -> );
Query OK, 0 rows affected (0.56 sec)

Now here is the query that will update the primary key to start from 1000 −

mysql> alter table PrimaryKey1000Demo auto_increment=1000;
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0

Now we have updated the start value to begin from 1000. Let us now insert some records to check the starting value of primary key. The query to insert record is as follows −

mysql> insert into PrimaryKey1000Demo values();
Query OK, 1 row affected (0.11 sec)

mysql> insert into PrimaryKey1000Demo values();
Query OK, 1 row affected (0.19 sec)

mysql> insert into PrimaryKey1000Demo values();
Query OK, 1 row affected (0.23 sec)

mysql> insert into PrimaryKey1000Demo values();
Query OK, 1 row affected (0.08 sec)

Display all records from the table using a select statement. The query is as follows −

mysql> select *from PrimaryKey1000Demo;

The following is the output displaying ProductID, our primary key to begin from 1000 −

+-----------+
| ProductId |
+-----------+
|      1000 |
|      1001 |
|      1002 |
|      1003 |
+-----------+
4 rows in set (0.00 sec)
raja
Published on 10-Jan-2019 14:32:49
Advertisements