确保该列遵循 Oracle 中的顺序
有没有办法限制列(例如“ID”)遵循创建的序列(例如“ID_SEQ”)?
如果没有自动约束,手动插入可能会使整个序列变得不正常。可以采取什么措施来解决这个问题?只需调用 NextVal 两次?
Is there any way of constraining a column (say "ID") to follow a created sequence (say "ID_SEQ")?
Without an automatic constraint, manual inserts might throw the entire sequence out-of-whack. What can be done to fix this? Just call NextVal twice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用触发器来完成此操作。例子:
You can use a trigger to accomplish this. Example:
没有办法为这种事情创建声明性约束。当然,您可以创建一个 BEFORE INSERT 行级触发器,该触发器将根据序列自动填充键(如果您担心不使用序列的临时插入,则忽略提供的任何值)。
如果您处于因某人临时插入创建的行且其键大于序列当前值而必须递增序列的情况,则您的两个选择是
There is no way to create a declarative constraint for this sort of thing. You could, of course, create a BEFORE INSERT row-level trigger that would automatically populate the key based on the sequence (ignoring any value that was provided if you're concerned about ad hoc inserts that don't use the sequence).
If you are in a situation where you have to increment a sequence because someone's ad hoc inserts created rows with keys greater than the current value of the sequence, your two options are