• <bdo id='K1ODl'></bdo><ul id='K1ODl'></ul>

  1. <legend id='K1ODl'><style id='K1ODl'><dir id='K1ODl'><q id='K1ODl'></q></dir></style></legend>

    <small id='K1ODl'></small><noframes id='K1ODl'>

      <tfoot id='K1ODl'></tfoot>

      <i id='K1ODl'><tr id='K1ODl'><dt id='K1ODl'><q id='K1ODl'><span id='K1ODl'><b id='K1ODl'><form id='K1ODl'><ins id='K1ODl'></ins><ul id='K1ODl'></ul><sub id='K1ODl'></sub></form><legend id='K1ODl'></legend><bdo id='K1ODl'><pre id='K1ODl'><center id='K1ODl'></center></pre></bdo></b><th id='K1ODl'></th></span></q></dt></tr></i><div id='K1ODl'><tfoot id='K1ODl'></tfoot><dl id='K1ODl'><fieldset id='K1ODl'></fieldset></dl></div>

      如何在 Oracle 上使用 AUTO_INCREMENT 创建 id?

      时间:2023-07-17

      <small id='9NftH'></small><noframes id='9NftH'>

      • <bdo id='9NftH'></bdo><ul id='9NftH'></ul>
            <tfoot id='9NftH'></tfoot>
              • <legend id='9NftH'><style id='9NftH'><dir id='9NftH'><q id='9NftH'></q></dir></style></legend>
                <i id='9NftH'><tr id='9NftH'><dt id='9NftH'><q id='9NftH'><span id='9NftH'><b id='9NftH'><form id='9NftH'><ins id='9NftH'></ins><ul id='9NftH'></ul><sub id='9NftH'></sub></form><legend id='9NftH'></legend><bdo id='9NftH'><pre id='9NftH'><center id='9NftH'></center></pre></bdo></b><th id='9NftH'></th></span></q></dt></tr></i><div id='9NftH'><tfoot id='9NftH'></tfoot><dl id='9NftH'><fieldset id='9NftH'></fieldset></dl></div>
                  <tbody id='9NftH'></tbody>
                本文介绍了如何在 Oracle 上使用 AUTO_INCREMENT 创建 id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 Oracle 中似乎没有 AUTO_INCREMENT 的概念,直到并包括版本 11g.

                It appears that there is no concept of AUTO_INCREMENT in Oracle, up until and including version 11g.

                如何在 Oracle 11g 中创建一个行为类似于自动增量的列?

                How can I create a column that behaves like auto increment in Oracle 11g?

                推荐答案

                Oracle 中没有auto_increment"或identity"列之类的东西从 Oracle 11g 开始.但是,您可以使用序列和触发器轻松对其进行建模:

                There is no such thing as "auto_increment" or "identity" columns in Oracle as of Oracle 11g. However, you can model it easily with a sequence and a trigger:

                表定义:

                CREATE TABLE departments (
                  ID           NUMBER(10)    NOT NULL,
                  DESCRIPTION  VARCHAR2(50)  NOT NULL);
                
                ALTER TABLE departments ADD (
                  CONSTRAINT dept_pk PRIMARY KEY (ID));
                
                CREATE SEQUENCE dept_seq START WITH 1;
                

                触发器定义:

                CREATE OR REPLACE TRIGGER dept_bir 
                BEFORE INSERT ON departments 
                FOR EACH ROW
                
                BEGIN
                  SELECT dept_seq.NEXTVAL
                  INTO   :new.id
                  FROM   dual;
                END;
                /
                

                更新:

                IDENTITY 列是现在可在 Oracle 12c 上使用:

                UPDATE:

                IDENTITY column is now available on Oracle 12c:

                create table t1 (
                    c1 NUMBER GENERATED by default on null as IDENTITY,
                    c2 VARCHAR2(10)
                    );
                

                或指定起始值和增量值,同时防止对标识列进行任何插入(GENERATED ALWAYS)(同样,仅适用于 Oracle 12c+)

                or specify starting and increment values, also preventing any insert into the identity column (GENERATED ALWAYS) (again, Oracle 12c+ only)

                create table t1 (
                    c1 NUMBER GENERATED ALWAYS as IDENTITY(START with 1 INCREMENT by 1),
                    c2 VARCHAR2(10)
                    );
                

                或者,Oracle 12 还允许使用序列作为默认值:

                Alternatively, Oracle 12 also allows to use a sequence as a default value:

                CREATE SEQUENCE dept_seq START WITH 1;
                
                CREATE TABLE departments (
                  ID           NUMBER(10)    DEFAULT dept_seq.nextval NOT NULL,
                  DESCRIPTION  VARCHAR2(50)  NOT NULL);
                
                ALTER TABLE departments ADD (
                  CONSTRAINT dept_pk PRIMARY KEY (ID));
                

                这篇关于如何在 Oracle 上使用 AUTO_INCREMENT 创建 id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 SQL Server 中添加 Active Directory 用户组作为登录名 下一篇:SQL Server,如何在不丢失数据的情况下创建表后设置自动增量?

                相关文章

              • <i id='48277'><tr id='48277'><dt id='48277'><q id='48277'><span id='48277'><b id='48277'><form id='48277'><ins id='48277'></ins><ul id='48277'></ul><sub id='48277'></sub></form><legend id='48277'></legend><bdo id='48277'><pre id='48277'><center id='48277'></center></pre></bdo></b><th id='48277'></th></span></q></dt></tr></i><div id='48277'><tfoot id='48277'></tfoot><dl id='48277'><fieldset id='48277'></fieldset></dl></div>

                <small id='48277'></small><noframes id='48277'>

                <tfoot id='48277'></tfoot>

                  1. <legend id='48277'><style id='48277'><dir id='48277'><q id='48277'></q></dir></style></legend>
                      <bdo id='48277'></bdo><ul id='48277'></ul>