1.由于mysql和oracle不同不能直接创建序列号所以我们存在一个表里,用来获取
DROP TABLE
IF EXISTS sequence;
CREATE TABLE
sequence
(
name VARCHAR(50) NOT NULL,
current_value BIGINT NOT NULL,
increment INT NOT NULL DEFAULT 1,
PRIMARY KEY (name)
)
ENGINE=InnoDB;
插入序列的名字和编号
INSERT INTO sequence VALUES ('SEQ_TRZ_MEMBER_NO',10000000000,1);
在表里如图所示
2.创建currval 函数
此函数为获取当前值
DROP FUNCTION IF EXISTS currval;
DELIMITER $$
CREATE FUNCTION currval (seq_name VARCHAR(50))
RETURNS BIGINT(20)
BEGIN
DECLARE value BIGINT ;
SET value = 0;
SELECT current_value into value
FROM sequence
WHERE name = seq_name;
RETURN value;
END $$
DELIMITER ;
执行
select currval('SEQ_TRZ_MEMBER_NO')
3.创建nextval函数
其实就是在当前值得基础上加上步长做更新
DROP FUNCTION IF EXISTS nextval;
DELIMITER $$
CREATE FUNCTION nextval (seq_name VARCHAR(50))
RETURNS BIGINT
CONTAINS SQL
BEGIN
UPDATE sequence
SET current_value = current_value + increment
WHERE name = seq_name;
RETURN currval(seq_name);
END $$
DELIMITER ;
执行
select nextval('SEQ_TRZ_MEMBER_NO')
注意:
如果有报错,可以设置一下
set global log_bin_trust_function_creators=TRUE;