oracle数据库与其他的数据库不太一样,比如在mysql里自动增长只要设定“auto_increment”即可。可是在oracle里就麻烦了。本文就说说在oracle里建立自动增长的字段。
1.建立一个简单的表
-- Create table create table USERINFO ( ID NUMBER not null, USERNAME VARCHAR2(25) not null, PASSWORD VARCHAR2(50) not null )
2.建立序列(sequence)
-- Create sequence create sequence USERINFO_AUTOINC minvalue 1 maxvalue 999999999999999999999999999 start with 2 increment by 1 nocache;
3.建立触发器(trigger)
create or replace trigger insert_for_autoinc before insert on userinfo for each row declare -- local variables here begin select USERINFO_AUTOINC.nextval into :new.ID from dual; end insert_for_autoinc;
4.SQL测试结果
insert into userinfo(username,password) values('test','test');
来自 http://weii.iteye.com/blog/347266#