这是一个简单的例子,不过已经能够表示如何实现这类功能了。 1、在scott用户下建立表dept(其实oracle缺省安装就已经生成了)
create table DEPT
(
DEPTNO NUMBER(2) not null,
DNAME VARCHAR2(14),
LOC VARCHAR2(13)
) 2、建立一个sequence
create sequence seq_dept
minvalue 1
start with 1
increment by 1
nocache; 3、为dept表建立一个insert trigger
create or replace trigger insert_dept
before insert on dept
for each row
declare
-- local variables here
begin
select seq_dept.nextval into :new.deptno from dual;
end insert_dept; 4、用insert语句测试 insert into dept(dname,loc) values('aaaaaa','bbbbb'); 用select * from dept察看结果
create table DEPT
(
DEPTNO NUMBER(2) not null,
DNAME VARCHAR2(14),
LOC VARCHAR2(13)
) 2、建立一个sequence
create sequence seq_dept
minvalue 1
start with 1
increment by 1
nocache; 3、为dept表建立一个insert trigger
create or replace trigger insert_dept
before insert on dept
for each row
declare
-- local variables here
begin
select seq_dept.nextval into :new.deptno from dual;
end insert_dept; 4、用insert语句测试 insert into dept(dname,loc) values('aaaaaa','bbbbb'); 用select * from dept察看结果
MAXVALUE 1.0E28 MINVALUE 1 NOCYCLE
NOCACHE NOORDER;CREATE OR REPLACE TRIGGER USERADD
BEFORE INSERT ON MEMBERRG
FOR EACH ROW
BEGIN
SELECT LPAD(USERID.NEXTVAL,4,'0') INTO :NEW.NUM FROM DUAL;
END;
create table test ( a number(2),b char);
create sequnece seq minvalue 1 increment by 1 start with 1 nocache;
insert into test values (seq.nextval,'0');
请大家指教,这个和上面高手讲的有什么区别吗?谢谢了
最简单的方法应该还是序列吧。我定义的序列一般都是1~999999999999999999的。呵呵。
习惯上NUMBER不需要指定(n)的