MySql中序列的应用和总结

Mysql中的序列主要用于主键,主键是递增的字段,不可重复。

Mysql与Oracle不同的是,它不支持原生态的sequence,需要用表和函数的组合来实现类似序列的功能。

1.首先创建序列的主表

/*
 Navicat Premium Data Transfer

 Source Server         : MySql_192.168.10.7
 Source Server Type    : MySQL
 Source Server Version : 100113
 Source Host           : 192.168.10.7:3306
 Source Schema         : config_fao_atp

 Target Server Type    : MySQL
 Target Server Version : 100113
 File Encoding         : 65001

 Date: 17/04/2020 17:38:44
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for sequence
-- ----------------------------
DROP TABLE IF EXISTS `sequence`;
CREATE TABLE `sequence`  (
  `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `current_value` int(11) NOT NULL,
  `increment` int(11) NOT NULL DEFAULT 1,
  PRIMARY KEY (`name`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of sequence
-- ----------------------------
INSERT INTO `sequence` VALUES (‘artableSeq‘, 10021776, 1);

SET FOREIGN_KEY_CHECKS = 1;

其次创建如下三个函数,它们的功能分别是:查询当前序列值、查询下一个序列值、设置序列的起始值。

/*
 Navicat Premium Data Transfer

 Source Server         : MySql_192.168.10.7
 Source Server Type    : MySQL
 Source Server Version : 100113
 Source Host           : 192.168.10.7:3306
 Source Schema         : config_fao_atp

 Target Server Type    : MySQL
 Target Server Version : 100113
 File Encoding         : 65001

 Date: 17/04/2020 17:38:44
*/
-- ----------------------------
-- Function structure for currval
-- ----------------------------
DROP FUNCTION IF EXISTS `currval`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `currval`(seq_name VARCHAR(50)) RETURNS int(11)
    DETERMINISTIC
BEGIN
DECLARE value INTEGER; 
SET value = 0; 
SELECT current_value INTO value 
FROM sequence
WHERE name = seq_name; 
RETURN value; 
END
;;
DELIMITER ;

-- ----------------------------
-- Function structure for nextval
-- ----------------------------
DROP FUNCTION IF EXISTS `nextval`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `nextval`(seq_name VARCHAR(50)) RETURNS int(11)
    DETERMINISTIC
BEGIN
UPDATE sequence
SET current_value = current_value + increment 
WHERE name = seq_name; 
RETURN currval(seq_name); 
END
;;
DELIMITER ;

-- ----------------------------
-- Function structure for setval
-- ----------------------------
DROP FUNCTION IF EXISTS `setval`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `setval`(seq_name VARCHAR(50), value INTEGER) RETURNS int(11)
    DETERMINISTIC
BEGIN
UPDATE sequence
SET current_value = value 
WHERE name = seq_name; 
RETURN currval(seq_name); 
END
;;
DELIMITER ;

3.最后初始化序列数据,测试序列功能。

----添加一个sequence名称和初始值,以及自增幅度  添加一个名为filegenSeq 的自增序列
INSERT INTO sequence VALUES (‘filegenSeq‘, 10000000, 1);

---设置指定sequence的初始值    这里设置filegenSeq 的初始值为10
SELECT SETVAL(‘filegenSeq‘, 10000000);

--查询指定sequence的当前值   这里是获取filegenSeq当前值
SELECT CURRVAL(‘filegenSeq‘);  

--查询指定sequence的下一个值  这里是获取filegenSeq下一个值
SELECT NEXTVAL(‘filegenSeq‘);

相关推荐