插入到 Oracle DB 表中的特定位置?

发布于 2024-11-09 01:08:16 字数 320 浏览 0 评论 0原文

假设我有一个表,包含以下数据:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杆小烟枪 2024-11-16 01:08:16

正如您所说,您不能依赖表中行的顺序(没有 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?

匿名的好友 2024-11-16 01:08:16

我将向表中添加一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文