Posted on 2013-07-19 21:39 
云云 阅读(2906) 
评论(1)  编辑  收藏  
			 
			
		 
		Sequence是数据库系统的特性,有的数据库有Sequence,有的没有。比如Oracle、DB2、PostgreSQL数据库有Sequence,MySQL、SQL Server、Sybase等数据库没有Sequence。
定义一个seq_test,最小值为10000,最大值为99999999999999999,从20000开始,增量的步长为1,缓存为20的循环排序Sequence。
Oracle的定义方法: 
create sequence seq_test
minvalue 10000
maxvalue 99999999999999999
start with 20000
increment by 1
cache 20
cycle
order;
Sequence与indentity的基本作用都差不多。都可以生成自增数字序列。
Sequence是数据库系统中的一个对象,可以在整个数据库中使用,和表没有任何关系;indentity仅仅是指定在表中某一列上,作用范围就是这个表。
一个表中可以有多个字段使用sequence字段
insert into temp(event_id,event_priority,event_status) values(sequence1.nextval, sequence1.nextval,sequence1.nextval);
mysql 实现sequence 
由于mysql不带sequence,所以要手写的,创建一张储存sequence的表(tb_sequence),然后手动插入一条数据 ,最后自定义一个函数来处理要增长的值。
1、创建表tb_sequence,用来存放sequence值:
 create table tb_sequence(name varchar(50) not null,current_value int not null,_increment int not null default 1, primary key(name));   
2 手动插入数据: 
  insert into tb_sequence values('userid',100,2);  
3、定义函数 _nextval: 
- DELIMITER //  
- create function _nextval(n varchar(50)) returns integer   
- begin  
- declare _cur int;  
- set _cur=(select current_value from tb_sequence where name= n);  
- update tb_sequence  
-  set current_value = _cur + _increment  
-  where name=n ;  
- return _cur;  
- end;  
检验结果 
 
select _nextval('userid');