当a表被查入一条数据,这条数据的id字段就自动赋值给这条数据的pid字段

解决方案 »

  1.   


    --1.先创建序列
    scott@YPCOST> create sequence orderNo_seq start with 100 increment by 1 maxvalue 999;序列已创建。scott@YPCOST> create table test(id number,name varchar2(20));表已创建。--2、再加触发器
    scott@YPCOST> ed
    已写入 file afiedt.bufcreate or replace trigger insert_tri
    before insert on test
    for each row
    declare
    begin
     select orderNo_seq.nextval into :new.id from dual;
    end;
    scott@YPCOST> /触发器已创建scott@YPCOST> insert into test(name) values('tom');已创建 1 行。scott@YPCOST> commit;提交完成。scott@YPCOST> select * from test;ID                   NAME
    -------------------- --------------------
                     100 tom                
      

  2.   


    --你的需求应该是这样的
    scott@YPCOST> create table test(id number,name varchar2(20));Table created.scott@YPCOST> 
    scott@YPCOST> create or replace trigger insert_tri
      2  before insert on test
      3  for each row
      4  begin
      5  :new.name:=:new.id;
      6  end;
      7  /Trigger created.scott@YPCOST> insert into test(id) values(1);1 row created.scott@YPCOST> commit;Commit complete.scott@YPCOST> select * from test;        ID NAME
    ---------- --------------------
             1 1