How to Define an Auto Increment Primary Key in Oracle

Much to the frustration of database administrators worldwide, prior to Oracle version 12c in mid-2014, Oracle simply had no inherent ability to inherently generate auto incrementing columns within a table schema. While the reasons for this design decision can only be guessed at, the good news is that even for users on older Oracle systems, there is a possible workaround to circumnavigate this pitfall and create your own auto incremented primary key column.

Creating a Sequence

The first step is to create a SEQUENCE in your database, which is a data object that multiple users can access to automatically generate incremented values. As discussed in the documentation, a sequence in Oracle prevents duplicate values from being created simultaneously because multiple users are effectively forced to “take turns” before each sequential item is generated.

For the purposes of creating a unique primary key for a new table, first we must CREATE the table we’ll be using:

CREATE TABLE books (
  id      NUMBER(10)    NOT NULL,
  title   VARCHAR2(100) NOT NULL
);

Next we need to add a PRIMARY KEY constraint:

ALTER TABLE books
  ADD (
    CONSTRAINT books_pk PRIMARY KEY (id)
  );

Finally, we’ll create our SEQUENCE that will be utilized later to actually generate the unique, auto incremented value.

CREATE SEQUENCE books_sequence;

Adding a Trigger

While we have our table created and ready to go, our sequence is thus far just sitting there but never being put to use. This is where TRIGGERS come in.

Similar to an event in modern programming languages, a TRIGGER in Oracle is a stored procedure that is executed when a particular event occurs.

Typically a TRIGGER will be configured to fire when a table is updated or a record is deleted, providing a bit of cleanup when necessary.

In our case, we want to execute our TRIGGER prior to INSERT into our books table, ensuring our SEQUENCE is incremented and that new value is passed onto our primary key column.

CREATE OR REPLACE TRIGGER books_on_insert
  BEFORE INSERT ON books
  FOR EACH ROW
BEGIN
  SELECT books_sequence.nextval
  INTO :new.id
  FROM dual;
END;

Here we are creating (or replacing if it exists) the TRIGGER named books_on_insert and specifying that we want the trigger to fire BEFORE INSERT occurs for the books table, and to be applicable to any and all rows therein.

The ‘code’ of the trigger itself is fairly simple: We SELECT the next incremental value from our previously created books_sequence SEQUENCE, and inserting that into the :new record of the books table in the specified .id field.

INSERT into books(title) values('4');
INSERT into books(title) values('5');

Note: The FROM dual part is necessary to complete a proper query but is effectively irrelevant. The dual table is just a single dummy row of data and is added, in this case, just so it can be ignored and we can instead execute the system function of our trigger rather than returning data of some kind.

IDENTITY Columns

IDENTITY columns were introduced in Oracle 12c, allowing for simple auto increment functionality in modern versions of Oracle.

Using the IDENTITY column is functionally similar to that of other database systems. Recreating our above books table schema in modern Oracle 12c or higher, we’d simply use the following column definition.

CREATE TABLE books (
  id      NUMBER        GENERATED BY DEFAULT ON NULL AS IDENTITY,
  title   VARCHAR2(100) NOT NULL
);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偶是江湖中人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值