在数据库中的行之间存储顺序的最佳方法是什么? [英] What is the best way to store the order between rows in the database?

查看:175
本文介绍了在数据库中的行之间存储顺序的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在克隆和扩展Google Tasks应用程序。我想要能够存储行的顺序。我在数据库中创建了一个名为rowNumber的字段。当用户在其他人的中间插入行时,我必须在许多其他行中更新此字段以实现所需的输出。有一个更好的方法吗?



该应用程序是为Google AppEngine(Python)制作的。

解决方案

使rowNumber成为一个浮点数。当用户插入行 x y 之间时,新行将获得rowNumber =(x.rowNumber + y.rowNumer)/ 2.



当您要移动一行时,只需按照目标位置相同的方式更新其rowNumber。



新行获取rowNumber。 MAX + 256。



编辑 Microsoft在SQL Server 2008中使用新的HierarchyId数据类型解决了这一点。您可以根据需要在两个几个之间插入多个次数 - 直到用完HierarchyId最大大小。


I am cloning and extending the Google Tasks application. I want to be able to store the rows order. I have created a field in the database, named rowNumber. When the user inserts a row in the middle of others, I have to update this field in many other rows to achieve the desired output. There is a better way to do it?

BTW the application is made for the Google AppEngine (Python).

解决方案

Make rowNumber a floating point number. When user inserts between rows x and y, the new row gets rowNumber = (x.rowNumber + y.rowNumer) / 2.

When you want to move a row, just update its rowNumber the same way based on target position.

New rows get rowNumber eg. MAX + 256.

Edit Microsoft solved this in SQL Server 2008 using new HierarchyId datatype. You can insert between 2 items almost as many times as you want - until you run out of HierarchyId max size.

这篇关于在数据库中的行之间存储顺序的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆