How to get the seed value of an identity column in MySQL?


For this, you can use SHOW VARIABLES command −

mysql> SHOW VARIABLES LIKE 'auto_inc%';

Output

This will produce the following output −

+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.95 sec)

You can control over AUTO_INCREMENT outside.

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY
   -> );
Query OK, 0 rows affected (0.94 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values();
Query OK, 1 row affected (0.44 sec)

mysql> insert into DemoTable values();
Query OK, 1 row affected (0.26 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+-----------+
| StudentId |
+-----------+
| 1         |
| 2         |
+-----------+
2 rows in set (0.00 sec)

Now you can control over the AUTO_INCREMENT −

mysql> alter table DemoTable AUTO_INCREMENT=1000;
Query OK, 0 rows affected (0.50 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.51 sec)

mysql> insert into DemoTable values();
Query OK, 1 row affected (1.37 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+-----------+
| StudentId |
+-----------+
|         1 |
|         2 |
|      1000 |
|      1001 |
+-----------+
4 rows in set (0.00 sec)

Updated on: 30-Jun-2020

426 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements