Auto-increment in Oracle without using a trigger

后端 未结 9 1966
野的像风
野的像风 2020-11-28 09:52

What are the other ways of achieving auto-increment in oracle other than use of triggers?

相关标签:
9条回答
  • 2020-11-28 10:21

    In addition to e.g. FerranB's answer:
    It is probably worth to mention that, as opposed to how auto_incement works in MySQL:

  • sequences work database wide, so they can be used for multiple tables and the values are unique for the whole database
  • therefore: truncating a table does not reset the 'autoincrement' functionaltiy

0 讨论(0)
  • 2020-11-28 10:22

    From 12c you can use an identity column, which makes explicit the link between table and auto-increment; there's no need for a trigger or a sequence. The syntax would be:

    create table <table_name> ( <column_name> generated as identity );
    
    0 讨论(0)
  • 2020-11-28 10:24
    SELECT max (id) + 1 
    FROM   table
    
    0 讨论(0)
  • 2020-11-28 10:32

    Create a sequence:

    create sequence seq;
    

    Then to add a value

    insert into table (id, other1, other2)
    values (seq.nextval, 'hello', 'world');
    

    Note: Look for oracle docs for more options about sequences (start value, increment, ...)

    0 讨论(0)
  • 2020-11-28 10:37

    If you don't really want to use a "trigger-based" solution, you can achieve the auto-increment functionality with a programmatical approach, obtaining the value of the auto increment key with the getGeneratedKeys() method.

    Here is a code snippet for your consideration:

    Statement stmt = null;
    ResultSet rs = null;
    
    stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                                    java.sql.ResultSet.CONCUR_UPDATABLE);
    
    stmt.executeUpdate("DROP TABLE IF EXISTS autoIncTable");
    
    stmt.executeUpdate("CREATE TABLE autoIncTable ("
                    + "priKey INT NOT NULL AUTO_INCREMENT, "
                    + "dataField VARCHAR(64), PRIMARY KEY (priKey))");
    
    stmt.executeUpdate("INSERT INTO autoIncTable  (dataField) "
                    + "values ('data field value')",
                    Statement.RETURN_GENERATED_KEYS);
    
    int autoIncKeyFromApi = -1;
    
    rs = stmt.getGeneratedKeys();
    
    if (rs.next()) {
        autoIncKeyFromApi = rs.getInt(1);
    }
    else {
        // do stuff here        
    }
    
    rs.close();
    

    source: http://forums.oracle.com/forums/thread.jspa?messageID=3368856

    0 讨论(0)
  • 2020-11-28 10:38

    A trigger to obtain the next value from a sequence is the most common way to achieve an equivalent to AUTOINCREMENT:

    create trigger mytable_trg
    before insert on mytable
    for each row
    when (new.id is null)
    begin
        select myseq.nextval into :new.id from dual;
    end;
    

    You don't need the trigger if you control the inserts - just use the sequence in the insert statement:

    insert into mytable (id, data) values (myseq.nextval, 'x');
    

    This could be hidden inside an API package, so that the caller doesn't need to reference the sequence:

    mytable_pkg.insert_row (p_data => 'x');
    

    But using the trigger is more "transparent".

    0 讨论(0)
  • 提交回复
    热议问题