In a MySQL schema, what is the meaning of “AUTO_INCREMENT=3”


In MySQL, AUTO_INCREMENT=3 tells that the inserted record will start from 3 not the default 1. Let us first create a sample table and set auto increment to 3:

mysql> create table Auto_incrementDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(20)
   -> )AUTO_INCREMENT=3;
Query OK, 0 rows affected (0.52 sec)

Following is the query to insert some records in the table using insert command:

mysql> INSERT INTO Auto_incrementDemo(Name) values('John');
Query OK, 1 row affected (0.12 sec)

mysql> INSERT INTO Auto_incrementDemo(Name) values('Larry');
Query OK, 1 row affected (0.15 sec)

mysql> INSERT INTO Auto_incrementDemo(Name) values('Bob');
Query OK, 1 row affected (0.15 sec)

Following is the query to display records from the table using select command:

mysql> select *from Auto_incrementDemo;

This will produce the following output

+----+-------+
| Id | Name  |
+----+-------+
| 3 | John   |
| 4 | Larry  |
| 5 | Bob    |
+----+-------+
3 rows in set (0.00 sec)

Look at the above sample output, id begins from 3, not 1, since we set AUTO_INCREMENT TO 3.

Updated on: 30-Jul-2019

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements