1、建一个表,SQL语句:create table test(id number primary key,name varchar2(20)not null);
2、创建一个序列给本表使用CREATE SEQUENCE test_id_seqINCREMENT BY 1 -- 每次加几个START WITH 1 -- 从1开始计数NOMAXVALUE -- 不设置最大值NOCYCLE -- 一直累加,不循环NOCACHE -- 不建缓冲区
3、创建触发器,让ID自动增加1create trigger test_trig beforeinsert on test for each row when (new.id is null)beginselect 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的数据