实现方法1:
建立一个最小为1,最大为nomaxvalue的一个序列号会自动循环的序列
create sequence 序列名
increment by 1
start with 1
nomaxvalue
nocycle;
当向表中插入数据时,SQL语句写法如下:
SQL> insert into 表名 values(序列名.nextval,列1值,列2值, ...);
当要重用该序列号时,有两种方法:
a. 在同一个sql块中重用:
SQL>insert into表名(序列名.currval, 列1值,列2值...);
b. 在存储进程中,将该值取到一个参数中:
SQL>select序列名.nextval into 参数名 from dual;
然后在重用该序列号的地方调用这个参数。
实现方法2:(利用触发器)
SQL> create sequence a_sequence
2 start with 1
3 increment by 1;
序列已创建。
SQL> create table t (n number ,v varchar2(10));
表已创建。
SQL> create or replace trigger t_trg
2 before insert or update on t
3 for each row
4 begin
5 select a_sequence.nextval into :new.n from dual;
6 end;
7 /
触发器已创建
SQL> insert into t values(111,'ok');
已创建 1 行。
SQL> select * from t;
N V
---------- ----------
1 ok
本文介绍了Oracle数据库中序列和触发器的使用方法。详细解释了如何创建序列,并通过示例展示了序列在插入数据时的应用。此外,还介绍了如何利用触发器自动填充表字段。
1893

被折叠的 条评论
为什么被折叠?



