标签:ext exists config ora varchar HERE insert sch inno
Mysql中的序列主要用于主键,主键是递增的字段,不可重复。/*
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 ;
----添加一个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‘);
标签:ext exists config ora varchar HERE insert sch inno
原文地址:https://blog.51cto.com/zhaoanan/2488233