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:
|
<<: Detailed explanation of the application of Docker underlying technology Namespace Cgroup
>>: How to use JavaScript to determine several common browsers through userAgent
After configuring the tabBar in the WeChat applet...
illustrate MySql Community Edition supports table...
Table of contents How to install and configure To...
Now 2016 server supports multi-site https service...
Table of contents The basic principle of MySQL ma...
1. One-click installation of Mysql script [root@u...
Introduction MySQL achieves high availability of ...
Table of contents Preface Analysis and solution o...
Remove the dotted box on the link Copy code The co...
Table of contents Preface - Vue Routing 1. The mo...
This article will examine the ES6 for ... of loop...
First time using docker to package and deploy ima...
question Recently I needed to log in to a private...
need: Implement dynamic display of option values ...
Table of contents 1. Example scenario 1.1. Set th...