Home > Database > Mysql Tutorial > body text

How to create and use sequences in MySQL?

WBOY
Release: 2023-08-26 20:05:14
forward
1331 people have browsed it

如何在 MySQL 中创建和使用序列?

To create a sequence in MySQL, you can use auto_increment on a column. it starts from The value is 1, and is incremented by 1 each time a record is inserted.

First, create a table with CREATE table. The query is as follows -

mysql> CREATE table SequenceDemo
-> (
-> SequenceId int auto_increment,
-> primary key(SequenceId)
-> );
Query OK, 0 rows affected (1.22 sec)
Copy after login

After creating the table, you can use the insert command to insert records The following is given -

mysql> INSERT into SequenceDemo values();
Query OK, 1 row affected (0.19 sec)

mysql> INSERT into SequenceDemo values();
Query OK, 1 row affected (0.14 sec)

mysql> INSERT into SequenceDemo values();
Query OK, 1 row affected (0.10 sec)

mysql> INSERT into SequenceDemo values();
Query OK, 1 row affected (0.12 sec)

mysql> INSERT into SequenceDemo values();
Query OK, 1 row affected (0.09 sec)
Copy after login

After inserting the record, you can use the select statement to display the record, that is Given below -

mysql> SELECT * from SequenceDemo;
Copy after login
Copy after login

Following is the output obtained -

+------------+
| SequenceId |
+------------+
| 1          |
| 2          |
| 3          |
| 4          |
| 5          |
+------------+
5 rows in set (0.00 sec)
Copy after login

The sequence can be set with the help of alter command. Its syntax is as follows Now, the above syntax is used in the following query to set the sequence value as -

alter table yourTableName auto_increment=Somevalue;
Copy after login

Now, the above syntax is used in the following query to set the sequence value as -

mysql> alter table SequenceDemo auto_increment = 500;
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
Copy after login

After that, records are inserted starting from the value 500 in the table. Given below-

mysql> INSERT into SequenceDemo values();
Query OK, 1 row affected (0.15 sec)

mysql> INSERT into SequenceDemo values();
Query OK, 1 row affected (0.15 sec)

mysql> INSERT into SequenceDemo values();
Query OK, 1 row affected (0.05 sec)
Copy after login

All the records can be displayed with the select statement as is given below-

mysql> SELECT * from SequenceDemo;
Copy after login
Copy after login

The output is as follows

+------------+
| SequenceId |
+------------+
| 1          |
| 2          |
| 3          |
| 4          |
| 5          |
| 500        |
| 501        |
| 502        |
+------------+
8 rows in set (0.00 sec)
Copy after login

As can be seen from the above output, in 5 After records, the sequence id starts from 500 and is Increase by 1.

The above is the detailed content of How to create and use sequences in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template