Home > Database > Mysql Tutorial > body text

How to implement auto-increment sequence in mysql

醉折花枝作酒筹
Release: 2021-07-12 17:14:28
forward
3745 people have browsed it

This article will introduce to you how to implement auto-increment sequence in mysql. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to implement auto-increment sequence in mysql

1. Create a sequence table

CREATE TABLE `sequence` (
  `name` varchar(50) COLLATE utf8_bin NOT NULL COMMENT '序列的名字',
  `current_value` int(11) NOT NULL COMMENT '序列的当前值',
  `increment` int(11) NOT NULL DEFAULT '1' COMMENT '序列的自增值',
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Copy after login

2. Create a function that takes the 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 ;
Copy after login

3. Create a function that takes the next one Value function

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 ;
Copy after login

4. Create a function that updates the 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 ;
Copy after login

5. Test to add an instance to execute sql

INSERT INTO sequence VALUES ('testSeq', 0, 1);-- 添加一个sequence名称和初始值,以及自增幅度

SELECT SETVAL('testSeq', 10);-- 设置指定sequence的初始值

SELECT CURRVAL('testSeq');-- 查询指定sequence的当前值

SELECT NEXTVAL('testSeq');-- 查询指定sequence的下一个值
Copy after login

Related recommendations: "mysql tutorial

The above is the detailed content of How to implement auto-increment sequence in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template