1. Create a sequence table CREATE TABLE `sequence` ( `name` varchar(50) COLLATE utf8_bin NOT NULL COMMENT 'Sequence name', `current_value` int(11) NOT NULL COMMENT 'Current value of the sequence', `increment` int(11) NOT NULL DEFAULT '1' COMMENT 'Sequence auto-increment', PRIMARY KEY (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; 2. Create – Function to get current value DROP FUNCTION IF EXISTS currval; DELIMITER $ CREATE FUNCTION currval (seq_name VARCHAR(50)) RETURNS INTEGER LANGUAGE SQL DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER COMMENT '' BEGIN DECLARE value INTEGER; SET value = 0; SELECT current_value INTO value FROM sequence WHERE name = seq_name; RETURN value; END $ DELIMITER ; 3. Create – function to get the next value DROP FUNCTION IF EXISTS nextval; DELIMITER $ CREATE FUNCTION nextval (seq_name VARCHAR(50)) RETURNS INTEGER LANGUAGE SQL DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER COMMENT '' BEGIN UPDATE sequence SET current_value = current_value + increment WHERE name = seq_name; RETURN currval(seq_name); END $ DELIMITER ; 4. Create – Function to update current value DROP FUNCTION IF EXISTS setval; DELIMITER $ CREATE FUNCTION setval (seq_name VARCHAR(50), value INTEGER) RETURNS INTEGER LANGUAGE SQL DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER COMMENT '' BEGIN UPDATE sequence SET current_value = value WHERE name = seq_name; RETURN currval(seq_name); END $ DELIMITER ; 5. Test adding instance to execute SQL INSERT INTO sequence VALUES ('testSeq', 0, 1);-- Add a sequence name and initial value, as well as the auto-increment rateSELECT SETVAL('testSeq', 10);-- Set the initial value of the specified sequenceSELECT CURRVAL('testSeq');-- Query the current value of the specified sequenceSELECT NEXTVAL('testSeq');-- Query the next value of the specified sequence This is the end of this article about the sample code for implementing auto-increment sequence in MySQL. For more information about auto-increment sequence in MySQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Understanding the CSS transform-origin property
>>: A detailed introduction to the Linux directory structure
This article uses an example to describe how to u...
Suddenly, I needed to build a private service for...
Today I learned to install MySQL, and some proble...
Table of contents Preface 1. What is selenium? 2....
Trigger Introduction A trigger is a special store...
Effect check address: Tour plan (uplanok.com) Cod...
Table of contents 1. Docker enables remote access...
HTML is a hybrid language used for publishing on ...
Background Recently, I encountered such a problem...
The pre element defines preformatted text. Text en...
####Management of input and output in the system#...
query_cache_limit query_cache_limit specifies the...
System: Ubuntu 16.04LTS 1\Download mysql-5.7.18-l...
Directly code: select 'bigint unsigned' a...
Table of contents ReactHooks Preface WhyHooks? Fo...