Oracle中是适用序列来实现这个功能的. 一个简单的例子: create table test (no number(4),name char(20));create sequence test_no increment by 1 start with 1 maxvalue 100 nocache nocycle;insert into test values(test_no.nextval,'Joan');insert into test values(test_no.nextval,'Peter');commit;
sequence 但是这个不会自动帮你插入,插入的时候要象1楼那样test_no.nextval
我一般都这么用:SQL> create table test (no number(4),name char(20)); 表已创建。create sequence test_seq increment by 1 start with 1 maxvalue 100000 nocache nocycle; create or replace trigger trigger_test before insert on test for each row begin select test_seq.nextval into :new.no from dual; end trigger_test; / SQL> insert into test values(null, 'hello');已创建 1 行。SQL> insert into test(name) values('iihero');已创建 1 行。
一个简单的例子:
create table test (no number(4),name char(20));create sequence test_no
increment by 1
start with 1
maxvalue 100
nocache
nocycle;insert into test values(test_no.nextval,'Joan');insert into test values(test_no.nextval,'Peter');commit;
但是这个不会自动帮你插入,插入的时候要象1楼那样test_no.nextval
表已创建。create sequence test_seq increment by 1 start with 1 maxvalue 100000 nocache nocycle;
create or replace trigger trigger_test
before insert on test
for each row
begin
select test_seq.nextval into :new.no from dual;
end trigger_test;
/ SQL> insert into test values(null, 'hello');已创建 1 行。SQL> insert into test(name) values('iihero');已创建 1 行。