create sequence t3_seq; create table t3(id$ integer constraint t3_pk primary key); create or replace trigger t3_autonumber before insert on t3 for each row begin if :new.id$ is null then select t3_seq.nextval into :new.id$ from dual; end if; end; / show errors;
insert into t3(id$) values (default); select * from t3;
ID$ ---------- 1
可以使用SYS_CONTEXT 值的集合中的默认值来填充字段,并收集有关某处一个会话的重要信息:
create table t4 ( when date default SYSDATE, db_domain varchar2(200) default SYS_CONTEXT('USERENV','DB_DOMAIN'), host varchar2(256) default SYS_CONTEXT('USERENV','HOST'), ip_address varchar2(256) default SYS_CONTEXT('USERENV','IP_ADDRESS'), language varchar2(256) default SYS_CONTEXT('USERENV','LANGUAGE'), protocol varchar2(200) default SYS_CONTEXT('USERENV','NETWORK_PROTOCOL'), terminal varchar2(200) default SYS_CONTEXT('USERENV','TERMINAL') ); insert into t4 (when) values (default); select * from t4;
WHEN --------- DB_DOMAIN ------------------------------------------------------------------------------ HOST ------------------------------------------------------------------------------ IP_ADDRESS ------------------------------------------------------------------------------ LANGUAGE ------------------------------------------------------------------------------ PROTOCOL ------------------------------------------------------------------------------ TERMINAL ------------------------------------------------------------------------------ 28-MAY-04 scott.bn MSHOME\SCOTT-LAP AMERICAN_AMERICA.AL32UTF8 SCOTT-LAP