09. Sequence in oracle
In Oracle, if you want to realize the self-increment operation of a column, you must use the sequence to complete
SQL statement to create sequence
create sequence seq_name
increment by n -- 每次增长几
start with n -- 从哪个值开始增长
maxvalue n|nomaxvalue 10^27 or -1 -- 最大值
minvalue n|no minvalue -- 最小值
cycle|nocycle -- 是否有循环
cache n|nocache -- 是否有缓存
Create sequence operation
-- 简单创建
create sequence my_sequence
increment by 2
start with 1
how to use
-- 注意,如果创建好序列之后,没有经过任何的使用,那么不能获取当前值
-- 查看序列的当前值
select my_sequence.currval from dual;
-- 提示错误:[72000][8002] ORA-08002: 序列 MY_SEQUENCE.CURRVAL 尚未在此会话中定义
-- 执行查看序列的下一个值
select my_sequence.nextval from dual;
-- 执行完再看当前值
select my_sequence.currval from dual;
-- 执行结果: 1
-- 然后再插入表中的sql中进行使用
insert into emp(empno,ename) values (my_sequence.nextval,'hehe');
select * from emp;