How to Create Sequence number in MySQL

In this MySQL Tutorial we will see how we can create sequence number in MySQL.

Sequence means if previous row has row id=10 then new row will get id=11 automatically through sequence.

Let’s see how we can generate the sequence in MySQL by taking example

MySQL Sequence Example – AUTO_INCREMENT

MySQL provide the feature where we can create sequence very easy by using AUTO_INCREMENT.

Let’s say we have visitor table and we want to generate visitor_id through sequence then we can AUTO_INCREMENT clause in visitor_id column of MySQL table.

create table visitor(visitor_id int PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(50), visit_date date);

Let’s insert few records into visitor table.

insert into visitors(name,visit_date) value("harry",now());
insert into visitor(name,visit_date) value("Tim",now());

mysql> select * from visitor;
| visitor_id | NAME  | visit_date |
|          1 | harry | 2024-01-08 |
|          2 | Tim   | 2024-01-08 |
2 rows in set (0.00 sec)

We have inserted 2 records into visitor table. visitor_id is populated automatically in MySQL table because we have created visitor_id as automatically aka sequence in MySQL.

Scroll to Top