How to keep ordering of records in a database table

和自甴很熟 提交于 2019-12-20 10:08:09

问题


i am creating a database table that is going to store menu links that will ultimately show up on a web page.

My issue is that i want to control the order of the menu items. I could have a field called order but everytime i have a new menu link i would have to insert the order and change all of the records with higher order to +1.

For example, lets say i want the links ( in this order):

Home  
About  
Products  
Shopping  

i could have a table called MenuLinks and have the columns: Name, Order

my data would look like this:

Name      Order  
Home      1  
About     2  
Products  3  
Shopping  4  

but if i wanted to now add a new link called ContactUs but i wanted to show up right under home.

can anyone think of a better way to store a list that requires ordering in a database table without this tedious maintenance effort.


回答1:


I feel this is related to the general problem of using an array vs a linked list. How about storing a foreign key referencing the next record in the same table? This is the linked list like approach.

For your example there are not too many tabs so an array based approach should work fine. But for someone having hundreds of records it may be useful to use a self-referential foreign key.

ID Name      NExT  
 1 Home      2  
 2 About     3  
 3 Products  4 
 4 Shopping  NULL

Adding and deleting rows will be akin to linked list insertion and deletion.

Update: Modified table

ID Name       NExT  
 1 Home       5  
 2 About      3  
 3 Products   4 
 4 Shopping   NULL
 5 Contact us 2

The order will be 1 > 5 > 2 > 3 > 4 as determined by the next column. You can also use a prev column which will make it similar to a doubly linked list.




回答2:


Without an ORDER BY, you can't guarantee the order of the data - typically, without an ORDER BY it will be based on insertion order.

Sadly, there's no convention that works well for a user customizable sort order.
One could get away with using analytic/windowing/ranking functions like ROW_NUMBER, but it depends on data and database support (MySQL doesn't support analytic functions, Oracle 9i+/PostgreSQL 8.4+/SQL Server 2005+ do). But analytic functions don't help if you want an entry starting with "B" to appear before "A"/etc.

Your options are to either use two statements to insert a single record:

UPDATE YOUR_TABLE
   SET sort_order = sort_order + 1
 WHERE sort_order >= 2

INSERT INTO YOUR_TABLE
  (value, sort_order)
VALUES('new value', 2)

...or delete the existing records, and re-insert the list in the new order.




回答3:


you should consider that when you use a linked list then when you want to reorder one of the items then you have to update other records as well and this needs to be done in a transaction which is not fast at all.(you need transaction because all the updates must be done completely or none of them must be updated)
there is an other solution for this problem that works on small lists.
to use this method you give each of your records a number. for example:

Name    Number
Home     5
About    10
Products 15
shopping 20

rows with smaller Number are at the begginnig of the list and the row with the biggest number will be the last item of your list now here is the trick, if you wanted to reorder the Products row and insert it between the Home and About all you have to do is to change the Number field of your Product to be equal to the number between Home and About Number
the Home number is 5 and the About number is 10 so the Number field of Product will be (5+10)/2 = 7.5

Name    Number
Home     5
About    10
Products 7.5
shopping 20

and now you can sort the final list, based on the Number field



来源:https://stackoverflow.com/questions/4115053/how-to-keep-ordering-of-records-in-a-database-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!