oracle 有“自动编号”数据类型吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6389641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 23:59:17  来源:igfitidea点击:

Does oracle have "auto number" data type

databaseoracle

提问by Adnan

I am amazed to see that oracle does not have "auto number" data type. Is there any way to use auto number data type as we have this data type in MS access?

我很惊讶地看到 oracle 没有“自动编号”数据类型。有没有办法使用自动编号数据类型,因为我们在 MS 访问中有这种数据类型?

回答by Michael Lowman

This blog postdescribes an option for this functionality.

这篇博文描述了此功能的一个选项。

Key elements are quoted below, but the post goes into more depth.

下面引用了关键要素,但这篇文章更深入。

create sequence test_seq 
start with 1 
increment by 1 
nomaxvalue;
create sequence test_seq 
start with 1 
increment by 1 
nomaxvalue;

Followed by

其次是

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

OR

或者

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

回答by Joel Slowik

In oracle you use a Sequence. You can have any number of sequences, and use any of them to assign a unique number to any field in any table or just call on one to assign a number to a variable.

在 oracle 中,您使用序列。您可以拥有任意数量的序列,并使用它们中的任何一个来为任何表中的任何字段分配一个唯一编号,或者仅调用一个来为变量分配一个编号。

SQL> CREATE SEQUENCE demo_seq INCREMENT BY 1 MAXVALUE 999999999999999999999999999 MINVALUE 0 NOCACHE;

Sequence created.

SQL> select demo_seq.nextval from dual;
   NEXTVAL                                                                      
----------                                                                      
         0  

SQL> select demo_seq.nextval from dual;

   NEXTVAL                                                                      
----------                                                                      
         1   

SQL> select demo_seq.nextval from dual;
   NEXTVAL                                                                      
----------                                                                      
         2   

SQL> select demo_seq.currval from dual;
   CURRVAL                                                                      
----------                                                                      
         2   

References:

参考:

http://www.techonthenet.com/oracle/sequences.php

http://www.techonthenet.com/oracle/sequences.php

http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_6015.htm

http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_6015.htm