1、建一个表,SQL语句:
create table test(
id number primary key,
name varchar2(20)not null
);
2、创建一个序列给本表使用
CREATE SEQUENCE test_id_seq
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
NOCACHE -- 不建缓冲区
3、创建触发器,让ID自动增加1
create trigger test_trig before
insert on test for each row when (new.id is null)
begin
select test_id_seq.nextval into:new.id from dual;
end;
4、最后可以直接插入数据了:
insert into test(name) values('wang')
这里就不用管ID了,会自动加1
.
5、查询我们插入的数据:
select * from test
可以看见已经成功插入一条id=1的数据