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
Long story short, today we will talk about using ...
pthread_create function Function Introduction pth...
There is a requirement to realize the shaking eff...
After spending half the night on it, I finally ma...
Table of contents 1. Browser local storage techno...
The examples in this article run on MySQL 5.0 and...
Table of contents 1 The common rules for creating...
This article uses examples to explain the princip...
Since PostgreSQL is compiled and installed, you n...
Table of contents 1. Block scope 1.1. let replace...
HTML web page hyperlink tag learning tutorial lin...
Original article, please indicate the author and ...
Overview The project was created successfully and...
This article shares the installation tutorial of ...
1. Command Introduction The stat command is used ...