失效链接处理 |
MySQL生成Oracle序列参考案例 PDF 下载
本站整理下载:
相关截图:
主要内容:
1.新建表:sequence
DROP TABLE IF EXISTS `sequence`;
CREATE TABLE `sequence` (
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_croatian_ci NOT
NULL,
`current_value` int(11) NOT NULL,
`increment` int(11) NOT NULL DEFAULT 1,
`valuelen` int(2) NOT NULL,
PRIMARY KEY (`name`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_croatian_ci
ROW_FORMAT = Compact;
SET FOREIGN_KEY_CHECKS = 1;
其中:name 相当于 Oracle 的序列名
current_value 存储当前值
increment 存储 步长 (一次调用增长多少)
valuelen 存储 长度 (例如:2-对应最多到 99 ,到 99 后再增加即返回
到 0)
2.创建三个函数:
2.1
CREATE DEFINER=`root`@`%` FUNCTION `currval`(seq_name VARCHAR(50))
RETURNS int(11)
BEGIN
DECLARE value INTEGER;
SET value = 0;
SELECT current_value INTO value
FROM sequence
WHERE name = seq_name;
RETURN value;
END
2.2
CREATE DEFINER=`root`@`%` FUNCTION `nextval`(seq_name VARCHAR(50))
RETURNS int(11)
BEGIN
DECLARE tmp int;
DECLARE tmp1 int;
select current_value + increment,valuelen into tmp,tmp1 from
sequence where name = seq_name;
|