Get the App
SLTechnology News&Howtos  ›  Database  › 

What is the function of mysql sequence

Shulou Source: shulou.com Published: 2022-06-01 01:54:51 10月04日 Update

This article mainly introduces the role of mysql sequence is what, has a certain reference value, need friends can refer to. I hope you all have a lot of success after reading this article. Let's take a look at it together.

MySQL sequence is a set of integers: 1, 2, 3, etc., because a data table can only have one field self-increasing primary key, if you want to achieve other fields also automatically increased, you can use MySQL sequence to achieve.

The easiest way to use sequences in MySQL is to define columns using MySQL AUTO_INCREMENT.

In the following example, a data table insect is created, and the id in the insect table can be automatically increased without specifying a value.

mysql> CREATE TABLE Insects> CREATE TABLE Insects - >(- > ( - > id INT UNSIGNED NOT NULL AUTO_INCREMENT,- > id INT UNSIGNED NOT NULL AUTO_INCREMENT , - > PRIMARY KEY(id),- > PRIMARY KEY (id ), - > name VARCHAR(30)NOT NULL,#type of insect- > name VARCHAR (30 )NOT NULL ,#type of insect - > Date NOT NULL,#date collection- > Date NOT NULL,#date collection - > origin VARCHAR (30) NOT NULL#where to collect- > origin VARCHAR (30) NOT NULL #where to collect);); query OK, 0 rows affected (0.02 s) query OK, 0 rows affected (0.02 s) mysql> INSERT INTO insect (id, name, date, origin) VALUES> INSERT INTO insect (id, name, date, origin) values - >(NULL,'Housefly,''2001-09- 10',' Kitchen'), - >(NULL,'Housefly,''2001-09- 10',' Kitchen'), - >(NULL,'millipede','2001-09- 10','lane'), - >(NULL,' millipede','2001-09- 10',' lane'), - >(NULL,'Grasshopper','2001-09- 10',' Front Yard ');- >(NULL,' Grasshopper ','2001-09- 10','Front Yard'); Query OK, 3 rows affected (0.02 seconds) Query OK, 3 rows affected (0.02 seconds) Records: 3 duplicates: 0 Warnings: 0 Records: 3 duplicates: 0 Warnings: 0 mysql> SELECT * FROM INSECTS ORDER BY id; SELECT * FROM INSECTS ORDER BY id ;+ -------------| id |name| date| source|| id |name | date | source |+ ---- + ------------- + ------------ + ------------ ++ ---- + ------------- + ------------ + ------------ +| 1 |housefly| 2001-09-10 |kitchen|| 1 |housefly | 2001 - 09 - 10 |kitchen | | 2 |millipedes| 2001-09-10 |Lane|| 2 |millipedes | 2001 - 09 - 10 |Lane | | 3 |Grasshopper| 2001-09-10 |front yard|| 3 |Grasshopper| 2001 - 09 - 10 |front yard| + -------------(0.00 seconds) 3 rows in the group (0.00 seconds) Thank you for reading this article carefully, I hope Xiaobian share what the role of mysql sequence is to help everyone, but also hope that everyone more support, pay attention to the industry information channel, encounter problems to find, detailed solutions waiting for you to learn!

Tags: Insects sequences kitchens houseflies grasshoppers driveways influences queries dates sources articles roles values names fields data datasheets methods values content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MySQL Shulou Technology Shulou Tech Info Redmi