Oracle中不使用触发器的自动增量

发布于 2024-07-09 13:07:19 字数 32 浏览 8 评论 0原文

oracle中除了使用触发器还有哪些方式实现自增?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

清引 2024-07-16 13:07:19

除了 FerranB 的回答之外:
可能值得一提的是,与 MySQL 中 auto_incement 的工作方式相反:

  • 序列在数据库范围内工作,因此它们可以用于多个表,并且值对于整个数据库是唯一的
  • 因此:截断表不会重置“自动增量”功能

  • 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

  • 何必那么矫情 2024-07-16 13:07:19

    从 12c 开始,您可以使用 身份列,这使得表和自动增量之间的联系变得明确; 不需要触发器或序列。 语法是:

    create table <table_name> ( <column_name> generated as identity );
    

    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 );
    
    脱离于你 2024-07-16 13:07:19

    据我回忆,我在 Oracle 的日子里,如果不使用 TRIGGER,就无法实现自动增量列。 任何使自动增量列的解决方案都涉及触发和序列(我假设您已经知道这一点,因此没有触发备注)。

    As far as I can recall from my Oracle days, you can't achieve Auto Increment columns without using TRIGGER. Any solutions out there to make auto increment column involves TRIGGER and SEQUENCE (I'm assuming you already know this, hence the no trigger remarks).

    原来是傀儡 2024-07-16 13:07:19

    创建序列:

    create sequence seq;
    

    然后添加一个值

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

    注意:查找 oracle 文档以获取有关序列的更多选项(起始值、增量……)

    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, ...)

    故人如初 2024-07-16 13:07:19

    您可以创建和使用预言序列。 语法和详细信息位于
    http://www.techonthenet.com/oracle/sequences.php

    另请阅读文章
    http://rnyb2.blogspot.com/2006/02 /潜在的陷阱-with-oracle-sequence.html
    了解其他 RDBMS 中 AUTONUMBER 的限制

    You can create and use oracle sequences. The syntax and details are at
    http://www.techonthenet.com/oracle/sequences.php

    Also read the article
    http://rnyb2.blogspot.com/2006/02/potential-pitfall-with-oracle-sequence.html
    to understand the limitations with respect to AUTONUMBER in other RDBMS

    与往事干杯 2024-07-16 13:07:19

    如果不需要序列号而只需要唯一 ID,则可以使用默认值 SYS_GUID()。 IE:

    CREATE TABLE xxx ( ID RAW(16) DEFAULT SYS_GUID() )
    

    If you don't need sequential numbers but only a unique ID, you can use a DEFAULT of SYS_GUID(). Ie:

    CREATE TABLE xxx ( ID RAW(16) DEFAULT SYS_GUID() )
    
    方圜几里 2024-07-16 13:07:19

    从序列中获取下一个值的触发器是实现与自动增量等效的最常见方法:

    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;
    

    如果控制插入,则不需要触发器 - 只需在插入语句中使用序列:

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

    这可以隐藏在API封装,使得调用者不需要引用序列:

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

    但是使用触发器更加“透明”。

    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".

    总以为 2024-07-16 13:07:19
    SELECT max (id) + 1 
    FROM   table
    
    SELECT max (id) + 1 
    FROM   table
    
    单调的奢华 2024-07-16 13:07:19

    如果您不想使用“基于触发器”的解决方案,您可以通过编程方式实现自动递增功能,通过 getGenerateKeys() 获取自动递增键的值方法。

    以下是供您考虑的代码片段:

    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();
    

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

    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

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文