Oracle中定义自动增量时插入触发器的行为

发布于 2024-09-04 04:14:58 字数 805 浏览 5 评论 0原文

我一直在寻找一种在 Oracle 中定义自动增量数据类型的方法,并在 Stack Overflow 上发现了这些问题:

  1. Autoincrement in Oracle
  2. Oracle数据库中的自增主键

使用自增的方式类型包括定义一个序列和一个触发器以使插入透明,其中插入触发器看起来像这样:

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;

我对此触发器的行为有一些疑问:

  1. 当提供的“id”值不同于 NULL 时,此触发器会做什么?
  2. “新”前面的冒号是什么意思?

我希望触发器插入新行,并将序列的下一个值作为 ID,无论提供的“new.id”值是什么。我想象 WHEN 语句使触发器仅在提供的 ID 为 NULL 时插入新行(否则它将不会插入或失败)。

我可以删除 WHEN 语句以便触发器始终使用序列的下一个值插入吗?

I have been looking for a way to define an autoincrement data type in Oracle and have found these questions on Stack Overflow:

  1. Autoincrement in Oracle
  2. Autoincrement Primary key in Oracle database

The way to use autoincrement types consists in defining a sequence and a trigger to make insertion transparent, where the insertion trigger looks so:

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;

I have some doubts about the behaviour of this trigger:

  1. What does this trigger do when the supplied value of "id" is different from NULL?
  2. What does the colon before "new" mean?

I want the trigger to insert the new row with the next value of the sequence as ID whatever the supplied value of "new.id" is. I imagine that the WHEN statement makes the trigger to only insert the new row if the supplied ID is NULL (and it will not insert, or will fail, otherwise).

Could I just remove the WHEN statement in order for the trigger to always insert using the next value of the sequence?

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

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

发布评论

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

评论(3

小嗷兮 2024-09-11 04:14:58

WHEN 条件指定触发器触发时必须为 TRUE 的条件。在此示例中,仅当新行具有 NULL IS 时触发器才会触发。当 ID 不为空时,触发器将不会触发,因此插入语句中给出的任何 ID 值都将被保留。

是的,如果您只是删除 WHEN 条件,那么触发器将始终触发,因此将始终为 ID 提供序列值。

The WHEN condition specifies a condition that must be TRUE for the trigger to fire. In this exampple, the trigger will only fire if the new row has a NULL IS. When the ID is not null, the trigger will not fire and so whatever value ID has been given in the insert statement will be left alone.

Yes, if you simply remove the WHEN condition then the trigger will always fire and so will always provide a sequence value for ID.

浮萍、无处依 2024-09-11 04:14:58
  1. 没有什么。这允许手动指定一个值。
  2. 它是列的新值的占位符。
  1. Nothing. That allows to specify a value manually.
  2. It's a placeholder for the new value of the column.
半透明的墙 2024-09-11 04:14:58

你有两种方法可以做:

如果表看起来像这样:

create table my_test (
id number, 
my_test data varchar2(255)
); 

并且你的序列是这样的:

create sequence test_seq 
start with 1 
increment by 1 
nomaxvalue;  

你可以创建一个像这样的触发器(没有像托尼·安德鲁斯所说的 When 语句)

create trigger test_trigger
before insert on my_test
for each row
begin
select test_seq.nextval into :new.id from dual;
end; 

或者你可以简单地使用这个,那么你就不需要需要一个触发器:

insert into my_test values(test_seq.nextval, 'voila!'); 

You have 2 methods you can do:

if the table looks like this:

create table my_test (
id number, 
my_test data varchar2(255)
); 

and your sequence is this:

create sequence test_seq 
start with 1 
increment by 1 
nomaxvalue;  

you can create a trigger like this (with no When statement like Tony Andrews said)

create trigger test_trigger
before insert on my_test
for each row
begin
select test_seq.nextval into :new.id from dual;
end; 

or you could just simply use this then you don't need a trigger:

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