mysql code to implement sequence function

mysql code to implement sequence function

MySQL implements sequence function

1. Create a sequence record table

CREATE TABLE `sys_sequence` (
 `seq_name` varchar(50) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
 `min_value` int(11) NOT NULL,
 `max_value` int(11) NOT NULL,
 `current_value` int(11) NOT NULL,
 `increment_value` int(11) NOT NULL DEFAULT '1',
 PRIMARY KEY (`seq_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

2. Establish sequence basic functions

DELIMITER $$
CREATE DEFINER=`root`@`%` FUNCTION `_nextval`(name varchar(50)) RETURNS int(11)
begin 
declare _cur int;
declare _maxvalue int; -- receive the maximum value declare _increment int; -- receive the number of increment steps set _increment = (select increment_value from sys_sequence where seq_name = name);
set _maxvalue = (select max_value from sys_sequence where seq_name = name);
set _cur = (select current_value from sys_sequence where seq_name = name); 
update sys_sequence -- Update the current value set current_value = _cur + increment_value 
where seq_name = name ; 
if(_cur + _increment >= _maxvalue) then -- Check if both have reached the maximum value update sys_sequence 
    set current_value = min_value 
    where seq_name = name ;
end if;
return _cur; 
end$$
DELIMITER ;

3. Insert the sequence you want to create

INSERT INTO `mydb`.`sys_sequence`
(`seq_name`,
`min_value`,
`max_value`,
`current_value`,
`increment_value`)
VALUES
('seq_name1', 1, 99999999, 1, 1);

4. Use sequence

select _nextval('seq_name1');

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Sequence implementation method based on MySQL
  • Tutorial on creating a self-increasing sequence in MySQL
  • How to set the automatic growth sequence SEQUENCE in MySQL

<<:  Detailed explanation of the application of Docker underlying technology Namespace Cgroup

>>:  How to use JavaScript to determine several common browsers through userAgent

Recommend

MySQL whole table encryption solution keyring_file detailed explanation

illustrate MySql Community Edition supports table...

Tomcat configuration and how to start it in Eclipse

Table of contents How to install and configure To...

Windows Server 2016 Quick Start Guide to Deploy Remote Desktop Services

Now 2016 server supports multi-site https service...

How does MySQL ensure master-slave consistency?

Table of contents The basic principle of MySQL ma...

One-click installation of MySQL 5.7 and password policy modification method

1. One-click installation of Mysql script [root@u...

A brief talk about MySQL semi-synchronous replication

Introduction MySQL achieves high availability of ...

CSS Skills Collection - Classics among Classics

Remove the dotted box on the link Copy code The co...

In-depth understanding of Vue-cli4 routing configuration

Table of contents Preface - Vue Routing 1. The mo...

ES6 loop and iterable object examples

This article will examine the ES6 for ... of loop...

How to use Docker to package and deploy images locally

First time using docker to package and deploy ima...

Solve the problem of Mac Docker x509 certificate

question Recently I needed to log in to a private...

Vue + element to dynamically display background data to options

need: Implement dynamic display of option values ...

JS calculates the probability of winning based on the prize weight

Table of contents 1. Example scenario 1.1. Set th...