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

Specific use of pthread_create in linux to create threads

pthread_create function Function Introduction pth...

CSS3 realizes the red envelope shaking effect

There is a requirement to realize the shaking eff...

CnBlogs custom blog style sharing

After spending half the night on it, I finally ma...

A brief discussion on the use of Web Storage API

Table of contents 1. Browser local storage techno...

How to use the MySQL authorization command grant

The examples in this article run on MySQL 5.0 and...

A brief discussion on which fields in Mysql are suitable for indexing

Table of contents 1 The common rules for creating...

Detailed explanation of the principles and usage of MySQL stored procedures

This article uses examples to explain the princip...

How to set up PostgreSQL startup on Ubuntu 16.04

Since PostgreSQL is compiled and installed, you n...

Detailed explanation of JS ES6 coding standards

Table of contents 1. Block scope 1.1. let replace...

HTML web page hyperlink tag

HTML web page hyperlink tag learning tutorial lin...

A must-read career plan for web design practitioners

Original article, please indicate the author and ...

MySQL 8.0.12 winx64 detailed installation tutorial

This article shares the installation tutorial of ...

Use of Linux stat command

1. Command Introduction The stat command is used ...