插入到 Oracle DB 表中的特定位置?
假设我有一个表,包含以下数据:
Name | Things
-------------
Foo | 5
Bar | 3
Baz | 8
如果我想插入一行,那么表的最终状态是:
Name | Things
-------------
Foo | 5
Qux | 6
Bar | 3
Baz | 8
这可能吗?
我知道我们通常不依赖于表中的行顺序,但我继承了一些恰好可以做到这一点的代码。如果我可以插入到某个位置,我就可以避免重大重构。
Suppose I have a table containing the following data:
Name | Things
-------------
Foo | 5
Bar | 3
Baz | 8
If I want to insert a row, so that the final state of the table is:
Name | Things
-------------
Foo | 5
Qux | 6
Bar | 3
Baz | 8
Is this possible?
I understand we don't typically rely on the order of rows in a table, but I've inherited some code that does precisely that. If I can insert to a location, I can avoid a significant refactor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所说,您不能依赖表中行的顺序(没有 ORDER BY)。
无论如何,我可能会重构代码 - 它有可能在将来的某个时候在没有警告的情况下崩溃 - 现在在受控情况下处理它肯定更好吗?
As you say, you can't rely on the order of rows in a table (without an ORDER BY).
I would probably refactor the code anyway - there's a chance it will break with no warning at some point in the future - surely better to deal with it now under controlled circumstances?
我将向表中添加一个 FLOAT 列,如果您想在该列中的值分别为 7.00000 和 8.000000 的行之间插入一行,则新行的值将为 7.50000。如果您想在 7.00000 和 7.50000 之间插入一行,新行将得到 7.25000,依此类推。然后,当您按该列排序时,您将获得所需顺序的列。改造相当容易。但并不像人们希望的那样强大。您应该撤销表中的所有更新/插入权限,并通过存储过程处理 I/O。
I would add a FLOAT column to the table and if you wanted to insert a row between the rows whose value in that column was 7.00000 and 8.000000 respectively, your new row would have value 7.50000. If you then wanted to insert a row between 7.00000 and 7.50000 the new row would get 7.25000, and so on. Then, when you order by that column, you get the columns in the desired order. Fairly easy to retrofit. But not as robust as one likes things to be. You should revoke all update/insert permissions from the table and handle I/O via a stored procedure.