请问oracle的自增字段是怎么实现的?尤其是我会删除其中的一些记录的时候,我用select   max(id)  from   tablename;   是否可以获得。我担心的是如果我删除一些字段,比如删除的一条现在值是最大的,插入一条新的记录后,新的记录的自增字段是值与刚删除的值相同。另外其它的数据库oracle是否有差别,目前要实现对多数据库的支持,起码要考虑oracle,sqlserver,mysql和db2.

解决方案 »

  1.   

    OPER@jf> create table test(aaa number);Table created.OPER@jf> create sequence seq_1 start with 1 nocache;Sequence created.OPER@jf> insert into test values(seq_1.nextval);1 row created.OPER@jf> /1 row created.OPER@jf> /1 row created.OPER@jf> commit;Commit complete.OPER@jf> select * from test;       AAA
    ----------
             1
             2
             3OPER@jf> delete from test where aaa=3;1 row deleted.OPER@jf> commit;Commit complete.OPER@jf> insert into test values(seq_1.nextval);1 row created.OPER@jf> commit;Commit complete.OPER@jf> select * from test;       AAA
    ----------
             1
             2
             4OPER@jf> 
      

  2.   

    oracle 的自增序号是独立的,自己创建一个就行了
    插入前将nextval记下来,然后该怎么做就怎么做了
    sqlserver,mysql都是自增字段,需要在操作之后取 SELECT LAST_INSERT_ID()
    db2没用过,不清楚
      

  3.   

    当然,select max()肯定是愚蠢的,
    两个人同时操作,你不是要犯傻了
      

  4.   

    不同的数据库对自增长的实现是不一样的.
    例如oracle使用sequence, sqlserver则是在create table时指定字段属性identity
      

  5.   

    我认为楼主处理数据这种思路不好,一条记录已经使用过这个ID号,删除后,为什么还要重新使用这个ID号?
    当然你一定要这样做也可以,就只能用select max(id) ... from tb  ,获取最ID的办法oracle里面有创建系列
    -- 序列号的产生 
    create sequence auto_id 
    minvalue 1 
    maxvalue 999999999999 
    start with 1 
    increment by 1 由这个系列何以获得最新的ID
    auto.nextval    下一个ID
    auto.currentval 当前ID
      

  6.   


    既然要考虑到多数据库平台, 那就建一个序列表吧, 然后再SELECT...FOR UPDATE.
    这种做法在Tom的那本Ora编程艺术中有介绍, 建议LZ参考一下
      

  7.   

    完整的看一下:http://topic.csdn.net/u/20081115/10/491a0efc-90cf-45b8-968e-29f3521f9279.html?seed=1462776239
      

  8.   

    create table tabid(id0 number(10));create or replace procedure p_insert_id(tbl in varchar2,int1 int,int2 int) as
        begin
        For i in int1..int2
        Loop
          insert into tabid values(i);
        End loop;
        commit;
        end;--插入记录时:
    insert into tableA(id) as select min(id0) from tabid;
    --其它的字段可以以update tableA的方式来做。这样的话可以用于oracle,sqlserver,mysql和db2。
      

  9.   

    sequence加触发器啊。create table a(id int ,name varchar2(10));
    create sequence seq;   ----序列
    create or replace trigger tr
    before insert on a
    for each row
    begin
    select seq.nextval into :new.a from dual;
    end;     ----触发器
    SQL> insert into a (b) values ('zhangs');1 row insertedSQL> insert into a (b) values ('lisi');1 row insertedSQL> insert into a (b) values ('fgggs');1 row insertedSQL> insert into a (b) values ('wangwun');1 row insertedSQL> select * from a;                                      A B
    --------------------------------------- ----------
                                          1 zhangs
                                          2 lisi
                                          3 fgggs
                                          4 wangwunSQL> delete from a where a=(select max(a)  from a);1 row deletedSQL> insert into a(b) values('nhhh');1 row insertedSQL> select * from a;                                      A B
    --------------------------------------- ----------
                                          1 zhangs
                                          2 lisi
                                          3 fgggs
                                          5 nhhh
      

  10.   

    我个人喜欢建立一个table,专门存储号码,然后select ..for update..