Home > Database > Mysql Tutorial > MySQL实现ORACLE序列(Sequence)的解决方案_MySQL

MySQL实现ORACLE序列(Sequence)的解决方案_MySQL

WBOY
Release: 2016-06-01 12:58:49
Original
1444 people have browsed it

背景:先总结一下MYSQL 自增长与ORACLE 序列的区别:

自增长只能用于表中的其中一个字段
自增长只能被分配给固定表的固定的某一字段,不能被多个表共用.
自增长会把一个未指定或NULL值的字段自动填上.

实验:我们可以用创建一个序列表,使用函数来获取序列的值

1. 新建序列表

 

drop table if exists tb_sequence;     
create table tb_sequence (         
seq_name        VARCHAR(50) NOT NULL, -- 序列名称         
current_val     INT         NOT NULL, -- 当前值         
increment_val   INT         NOT NULL    DEFAULT 1, -- 步长(跨度)         
PRIMARY KEY (seq_name)   );  
Copy after login

2、 新增一个序列

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
INSERT INTO tb_sequence VALUES ('seq_test1_num1', '0', '1'); INSERT INTO tb_sequence VALUES ('seq_test1_num2', '0', '2'); 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
3、创建 函数 用于获取序列当前值(v_seq_name 参数值 代表序列名称)

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
create function currval(v_seq_name VARCHAR(50)) returns integer begin declare value integer; set value = 0; select current_val into value from tb_sequence where seq_name = v_seq_name; return value; end; 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
4、查询当前值

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
select currval('seq_test1_num1'); 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
5、创建 函数 用于获取序列下一个值(v_seq_name 参数值 代表序列名称)

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
create function nextval (v_seq_name VARCHAR(50)) returns integer begin update tb_sequence set current_val = current_val + increment_val where seq_name = v_seq_name; return currval(v_seq_name); end; 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
6、 查询下一个值

select nextval('seq_test1_num1'); 
Copy after login

7、新建表 用于测试的表

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
DROP TABLE IF EXISTS `test1`; CREATE TABLE `test1` ( `name` varchar(255) NOT NULL, `value` double(255,0) DEFAULT NULL, `num1` int(11) DEFAULT NULL, `num2` int(11) DEFAULT NULL, PRIMARY KEY (`name`) ); 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
8、 新建触发器 插入新纪录前给自增字段赋值实现字段自增效果

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
CREATE TRIGGER `TRI_test1_num1` BEFORE INSERT ON `test1` FOR EACH ROW BEGIN set NEW.num1 = nextval('seq_test1_num1'); set NEW.num2 = nextval('seq_test1_num2'); END
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
9、 最后测试自增效果

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
INSERT INTO test1 (name, value) VALUES ('1', '111'); INSERT INTO test1 (name, value) VALUES ('2', '222'); INSERT INTO test1 (name, value) VALUES ('3', '333'); INSERT INTO test1 (name, value) VALUES ('4', '444'); 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
10、实验结果

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
SELECT * FROM test1; 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
\


Related labels:
source:php.cn
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