oracle中没有自增类型的字段,但是提供了一个叫做序列的数据库对象,可以用这个来完成自增类型。
create table mytable (id number(10));
create sequence myseq;
insert into mytable (id) values myseq.nextval;
create table mytable (id number(10));
create sequence myseq;
insert into mytable (id) values myseq.nextval;
软件环境:
1、Windows 2000 personal +ORACLE 8.1.7客户端
2、ORACLE安装路径为:D:\ oracle实现方法:
建立一个最小为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;
然后在重用该序列号的地方调用这个参数。
直接在dba里面可以又图形化的界面可以增加序列
第一步:创建SEQUENCE
create sequence s_country_id increment by 1 start with 1 maxvalue 999999999;
第二步:创建一个基于该表的before insert 触发器,在触发器中使用该SEQUENCE
create or replace trigger bef_ins_t_country_define
before insert on t_country_define
referencing old as old new as new for each row
begin
new.country_id=s_country_id.nextval;
end;