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

Oracle 11g及更早版本中没有 AUTO_INCREMENT 功能,但可以通过序列和触发器模拟。在Oracle 12c及以上版本,可以直接使用 Identity 列创建自增键。本文介绍了不同版本Oracle创建自增ID的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:How to create id with AUTO_INCREMENT on Oracle?

It appears that there is no concept of AUTO_INCREMENT in Oracle, up until and including version 11g. 直到并包括11g版,Oracle中似乎都没有AUTO_INCREMENT的概念。

How can I create a column that behaves like auto increment in Oracle 11g? 如何在Oracle 11g中创建行为类似于自动增量的列?


#1楼

参考:https://stackoom.com/question/lOhN/如何在Oracle上使用AUTO-INCREMENT创建ID


#2楼

There is no such thing as "auto_increment" or "identity" columns in Oracle as of Oracle 11g . 从Oracle 11g开始,Oracle中没有诸如“ auto_increment”或“ identity”列之类的东西。 However, you can model it easily with a sequence and a trigger: 但是,您可以使用序列和触发器轻松对其进行建模:

Table definition: 表定义:

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;

Trigger definition: 触发定义:

CREATE OR REPLACE TRIGGER dept_bir 
BEFORE INSERT ON departments 
FOR EACH ROW

BEGIN
  SELECT dept_seq.NEXTVAL
  INTO   :new.id
  FROM   dual;
END;
/

UPDATE: 更新:

IDENTITY column is now available on Oracle 12c: Oracle 12c上现在提供了IDENTITY列:

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

or specify starting and increment values, also preventing any insert into the identity column ( GENERATED ALWAYS ) (again, Oracle 12c+ only) 或指定起始值和增量值,也防止任何插入到身份列中( GENERATED ALWAYS )(同样,仅适用于Oracle 12c +)

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

Alternatively, Oracle 12 also allows to use a sequence as a default value: 另外,Oracle 12还允许使用序列作为默认值:

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

#3楼

SYS_GUID returns a GUID-- a globally unique ID. SYS_GUID返回GUID-全局唯一ID。 A SYS_GUID is a RAW(16) . SYS_GUIDRAW(16) It does not generate an incrementing numeric value. 它不会生成递增的数值。

If you want to create an incrementing numeric key, you'll want to create a sequence. 如果要创建递增数字键,则需要创建一个序列。

CREATE SEQUENCE name_of_sequence
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

You would then either use that sequence in your INSERT statement 然后,您可以在INSERT语句中使用该序列

INSERT INTO name_of_table( primary_key_column, <<other columns>> )
  VALUES( name_of_sequence.nextval, <<other values>> );

Or you can define a trigger that automatically populates the primary key value using the sequence 或者,您可以定义一个触发器,使用该序列自动填充主键值

CREATE OR REPLACE TRIGGER trigger_name
  BEFORE INSERT ON table_name
  FOR EACH ROW
BEGIN
  SELECT name_of_sequence.nextval
    INTO :new.primary_key_column
    FROM dual;
END;

If you are using Oracle 11.1 or later, you can simplify the trigger a bit 如果您使用的是Oracle 11.1或更高版本,则可以简化触发器

CREATE OR REPLACE TRIGGER trigger_name
  BEFORE INSERT ON table_name
  FOR EACH ROW
BEGIN
  :new.primary_key_column := name_of_sequence.nextval;
END;

If you really want to use SYS_GUID 如果您真的想使用SYS_GUID

CREATE TABLE table_name (
  primary_key_column raw(16) default sys_guid() primary key,
  <<other columns>>
)

#4楼

oracle has sequences AND identity columns in 12c oracle在12c中具有序列AND身份列

http://www.oracle-base.com/articles/12c/identity-columns-in-oracle-12cr1.php#identity-columns http://www.oracle-base.com/articles/12c/identity-columns-in-oracle-12cr1.php#identity-columns

I found this but not sure what rdb 7 is http://www.oracle.com/technetwork/products/rdb/0307-identity-columns-128126.pdf 我发现了这一点,但不确定什么是rdb 7 http://www.oracle.com/technetwork/products/rdb/0307-identity-columns-128126.pdf


#5楼

Oracle Database 12c introduced Identity, an auto-incremental (system-generated) column. Oracle Database 12c引入了Identity,这是一个自动增量(系统生成的)列。 In the previous database versions (until 11g), you usually implement an Identity by creating a Sequence and a Trigger. 在以前的数据库版本(直到11g)中,通常通过创建序列和触发器来实现身份。 From 12c onward, you can create your own Table and define the column that has to be generated as an Identity. 从12c开始,您可以创建自己的表并定义必须作为身份生成的列。

The following article explains how to use it: 下面的文章解释了如何使用它:

Identity columns - A new entry in Oracle Database 12c 标识列-Oracle Database 12c中的新条目


#6楼

Assuming you mean a column like the SQL Server identity column? 假设您指的是SQL Server标识列之类的列?

In Oracle, you use a SEQUENCE to achieve the same functionality. 在Oracle中,您可以使用SEQUENCE实现相同的功能。 I'll see if I can find a good link and post it here. 我将查看是否可以找到一个很好的链接并将其发布在这里。

Update: looks like you found it yourself. 更新:看起来像您自己找到的。 Here is the link anyway: http://www.techonthenet.com/oracle/sequences.php 无论如何,这里是链接: http : //www.techonthenet.com/oracle/sequences.php

### 修改 `userbehavior` 表以设置自增主键 在 SQL Server 中,可以通过以下方式将现有的表字段修改为自增主键。需要注意的是,在执行这些操作之前,应确保该字段的数据类型适合作为自增主键(通常为整数类型),并且不存在重复值或 NULL 值。 以下是实现这一目标的具体方法: #### 1. 确保 `id` 字段满足条件 在将其设为主键前,必须确认 `id` 列不包含任何重复值或 NULL 值[^4]。可以运行以下查询验证: ```sql SELECT COUNT(*), COUNT(DISTINCT id) FROM userbehavior; ``` 如果两者的计数值不同,则说明存在重复值或 NULL 值,需要先清理数据。 #### 2. 删除现有主键约束(如果有) 如果 `id` 已经是主键,那么需要先删除其主键约束。假设当前主键名为 `PK_userbehavior`,则可使用如下命令: ```sql ALTER TABLE userbehavior DROP CONSTRAINT PK_userbehavior; ``` #### 3. 添加自增属性并重新定义主键 由于无法直接通过 `ALTER TABLE` 将已有列更改为身份列(Identity Column),因此需要创建一个新的临时表来迁移数据,并重建具有自增特性的 `id` 列。具体步骤如下: ##### 创建新表 新建一个与原表结构相似的新表,其中 `id` 被定义为自增主键: ```sql CREATE TABLE temp_userbehavior ( id INT IDENTITY(1,1) PRIMARY KEY, -- 其他字段保持不变 column1 datatype, column2 datatype, ... ); ``` ##### 数据迁移 将原始表中的数据迁移到新的临时表中。注意跳过原有的 `id` 列,因为它是自动填充的: ```sql INSERT INTO temp_userbehavior (column1, column2, ...) SELECT column1, column2, ... FROM userbehavior; ``` ##### 替换旧表 完成数据迁移后,删除原来的 `userbehavior` 表并将临时表重命名为正式名称: ```sql DROP TABLE userbehavior; EXEC sp_rename 'temp_userbehavior', 'userbehavior'; ``` 至此,`id` 列已被成功配置为自增主键。 --- ### 注意事项 - 如果尝试直接修改已经存在的非 Identity 列为其赋予 Identity 属性会失败,这是因为 SQL Server 不支持这种类型的变更。 - 对于 Oracle 数据库而言,虽然也提供序列对象辅助生成唯一编号的功能,但并不像 SQL Server 那样内置了专门的身份列概念;所以针对跨平台移植时需特别留意差异性处理逻辑[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值