Oracle中不使用触发器的自动增量
oracle中除了使用触发器还有哪些方式实现自增?
What are the other ways of achieving auto-increment in oracle other than use of triggers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
oracle中除了使用触发器还有哪些方式实现自增?
What are the other ways of achieving auto-increment in oracle other than use of triggers?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
除了 FerranB 的回答之外:
可能值得一提的是,与 MySQL 中 auto_incement 的工作方式相反:
In addition to e.g. FerranB's answer:
It is probably worth to mention that, as opposed to how auto_incement works in MySQL:
从 12c 开始,您可以使用 身份列,这使得表和自动增量之间的联系变得明确; 不需要触发器或序列。 语法是:
From 12c you can use an identity column, which makes explicit the link between table and auto-increment; there's no need for a trigger or a sequence. The syntax would be:
据我回忆,我在 Oracle 的日子里,如果不使用 TRIGGER,就无法实现自动增量列。 任何使自动增量列的解决方案都涉及触发和序列(我假设您已经知道这一点,因此没有触发备注)。
As far as I can recall from my Oracle days, you can't achieve Auto Increment columns without using TRIGGER. Any solutions out there to make auto increment column involves TRIGGER and SEQUENCE (I'm assuming you already know this, hence the no trigger remarks).
创建序列:
然后添加一个值
注意:查找 oracle 文档以获取有关序列的更多选项(起始值、增量……)
Create a sequence:
Then to add a value
Note: Look for oracle docs for more options about sequences (start value, increment, ...)
您可以创建和使用预言序列。 语法和详细信息位于
http://www.techonthenet.com/oracle/sequences.php
另请阅读文章
http://rnyb2.blogspot.com/2006/02 /潜在的陷阱-with-oracle-sequence.html
了解其他 RDBMS 中 AUTONUMBER 的限制
You can create and use oracle sequences. The syntax and details are at
http://www.techonthenet.com/oracle/sequences.php
Also read the article
http://rnyb2.blogspot.com/2006/02/potential-pitfall-with-oracle-sequence.html
to understand the limitations with respect to AUTONUMBER in other RDBMS
如果不需要序列号而只需要唯一 ID,则可以使用默认值 SYS_GUID()。 IE:
If you don't need sequential numbers but only a unique ID, you can use a DEFAULT of SYS_GUID(). Ie:
从序列中获取下一个值的触发器是实现与自动增量等效的最常见方法:
如果控制插入,则不需要触发器 - 只需在插入语句中使用序列:
这可以隐藏在API封装,使得调用者不需要引用序列:
但是使用触发器更加“透明”。
A trigger to obtain the next value from a sequence is the most common way to achieve an equivalent to AUTOINCREMENT:
You don't need the trigger if you control the inserts - just use the sequence in the insert statement:
This could be hidden inside an API package, so that the caller doesn't need to reference the sequence:
But using the trigger is more "transparent".
如果您不想使用“基于触发器”的解决方案,您可以通过编程方式实现自动递增功能,通过
getGenerateKeys()
获取自动递增键的值方法。以下是供您考虑的代码片段:
来源:http://forums.oracle .com/forums/thread.jspa?messageID=3368856
If you don't really want to use a "trigger-based" solution, you can achieve the auto-increment functionality with a programmatical approach, obtaining the value of the auto increment key with the
getGeneratedKeys()
method.Here is a code snippet for your consideration:
source: http://forums.oracle.com/forums/thread.jspa?messageID=3368856