且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在主键列中重用 Oracle 序列间隙?

更新时间:2022-01-05 19:40:02

你的意思是,序列会返回一个间隙"范围内的值吗?我不这么认为,除非您出于某种原因删除/重新创建它.我想您可以编写某种函数来查找表中的 PK 间隙,然后将这些间隙范围保存到另一个表中,并使用间隙表滚动"您自己的序列函数.十分难看.试图恢复"这些差距听起来像是避免不可避免的绝望尝试 - 您的 java PK 数据类型应该与 DB 数据类型保持一致.很久以前,我在一个 VB 应用程序中遇到了同样的问题,该应用程序的类键定义为 16 位整数,并且序列超过 32K,不得不将变量更改为 Long.我说,咬紧牙关,进行转换.现在有点痛苦,以后会为您节省很多持续的痛苦.只是我的意见.

Do you mean, will the sequence ever return a value that is in a "gap" range? I don't think so, unless you drop/re-create it for some reason. I guess you could write a function of some sorts to find the PK gaps in your table, then save those gap ranges to another table, and "roll" your own sequence function using the gap table. Very ugly. Trying to "recover" these gaps just sounds like a desperate attempt to avoid the unavoidable - your java PK data type should have aligned with the DB data type. I had the same problem a long time ago with a VB app that had a class key defined as 16-bit integer, and the sequence exceeded 32K, had to change the variables to a Long. I say, bite the bullet, and make the conversion. A little pain now, will save you a lot of ongoing pain later. Just my opinion.