建一个sequence,然后写个触发器,就可以了

解决方案 »

  1.   

    不会这么麻烦吧?能不能像mysql:
    create table newtable(id int(5) auto_increment not null,descid int(3));这样只要用“auto_increment”就可以了?谢谢!
      

  2.   

    szsusuia(丁曼)说的一点多不错,ORACLE中没有自增型字段。请丁曼再给个示例吧!
      

  3.   

    create sequence sq_name
    increment by 1
    start with 1
    maxvalue 99999
    cycle;
    创建序列,从1到99999循环,以1为递增单位
    以此序列为列值,
    insert into tab_name
    values(sq_name.nextval,.....); 
      

  4.   

    mashansj(风影)写的很正确,不过一般我们都是用该字段做主键。所以都不cycle.
      

  5.   

    szsusuia(丁曼) is right
    刚刚又学了一招,可以自动插入该列
    先建序列:
    create sequence sq_name
    increment by 1
    start with 1;
    再建触发器:
    create or replace trigger tri_name
    before insert or update on table_name
    for each row
    begin
    select sq_name.nextval
    into :new.id          --id,自动增加的列名
    from dual;
    end tri_name;
    插入时,直接插入其他列。该列是自动插入的,如果你在插入时给该列赋值是无效的。