1 use a sequence and a trigger:--create a sequence
CREATE SEQUENCE sequencename INCREMENT BY 1 START WITH 1 MAXVALUE 999999999;--create a trigger ( before insert ) base on the table,and use the sequence in itCREATE OR REPLACE TRIGGER triggername
BEFORE INSERT ON on tablename
REFERENCING old AS old new as new for each row
BEGIN
   SELECT sequencename.nextval into :new.fieldname(the filed that you want it to autoadd) FROM dual;
END;
/2 use a trigger directly:CREATE OR REPLACE TRIGGER triggername
  BEFORE INSERT ON tablename
  FOR EACH ROWDECLARE
  variablename NUMBER;BEGIN
   SELECT MAX(fieldname) INTO variablename FROM tablename; 
   :NEW.fieldname:=variablename+1;
END;
/

解决方案 »

  1.   

    1 use a sequence and a trigger
    --create a sequence
    CREATE SEQUENCE sequencename INCREMENT BY 1 START WITH 1 MAXVALUE 999999999;--create a trigger ( before insert ) base on the table,and use the sequence in itCREATE OR REPLACE TRIGGER triggername
    BEFORE INSERT ON on tablename
    REFERENCING old AS old new as new for each row
    BEGIN
       SELECT sequencename.nextval into :new.fieldname(the filed that you want it to autoadd) FROM dual;
    END;
    /
    2 use trigger directly:
    CREATE OR REPLACE TRIGGER triggername
      BEFORE INSERT ON tablename
      FOR EACH ROWDECLARE
      variablename NUMBER;BEGIN
       SELECT MAX(fieldname) INTO variablename FROM tablename; 
       :NEW.fieldname:=variablename+1;
    END;
    /
      

  2.   

    1 use a sequence and a trigger
    --create a sequence
    CREATE SEQUENCE sequencename INCREMENT BY 1 START WITH 1 MAXVALUE 999999999;--create a trigger ( before insert ) base on the table,and use the sequence in itCREATE OR REPLACE TRIGGER triggername
    BEFORE INSERT ON on tablename
    REFERENCING old AS old new as new for each row
    BEGIN
       SELECT sequencename.nextval into :new.fieldname(the filed that you want it to autoadd) FROM dual;
    END;
    /
    2 use a trigger directly:
    CREATE OR REPLACE TRIGGER triggername
      BEFORE INSERT ON tablename
      FOR EACH ROWDECLARE
      variablename NUMBER;BEGIN
       SELECT MAX(fieldname) INTO variablename FROM tablename; 
       :NEW.fieldname:=variablename+1;
    END;
    /
      

  3.   

    例子:
    create sequence seq_name
    minvalue 1
    maxvalue 99999999999999999999999
    increment by 1
    start with 1;
      

  4.   

    如果你以前用过sql server 2000或者access等数据库,很可能习惯使用一个自增一的字段作为主键。但是,在Oracle中并不存在对应的字段,但可以使用序列达到相同的目的,不过相对复杂一些。首先,向楼上和楼上的楼上说的一样,建立一个sequence;
    然后,在向表中插入数据的时候,取得sequence的next伪列的值,作为该字段的值进行插入;insert myTable values (seq_name.next(),other,columns);