首页 > 代码库 > oracle 序列

oracle 序列

--序列
-- 使用序列是实现自增 在mysql中由idtify自增 oracle里没有这个自增函数 只能创建序列
drop table student;
create table student(
sid int primary key,
sname varchar2(10)
)

select *from student;

--为student创建序列
drop sequence stu_seq_1;
create sequence stu_seq_1
start with 1000
increment by 1
NOMAXVALUE
NOMINVALUE;

select stu_seq_1.nextval from dual; --nextval 查询下一个序列并且将创建出来
select stu_seq_1.currval from dual; --currval 查询当前序列
insert into student values(stu_seq_1.nextval,‘a‘);
insert into student values(stu_seq_1.nextval,‘b‘);
select *from student;

--修改序列
--语法 alert sequence 序列名 要修改的值
alter sequence stu_seq_1 increment by 10;
insert into student values(stu_seq_1.nextval,‘c‘);
insert into student values(stu_seq_1.nextval,‘d‘);
--删除序列

drop sequence stu_seq_1;

insert into student values(stu_seq_1.nextval,‘T‘);--报错

oracle 序列