Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

11
  • 7
    "The drawback of this is that a lot of queries need to be done each time a song is moved" ?! - update songorder set order = order - 1 where order >= 12 & order <= 42; update songorder set order = 42 where id = 123; - that's two updates - not thirty. Three if you want to put a unique constraint on order. Commented Dec 8, 2015 at 22:03
  • 2
    Use option one unless you know for a fact you need something else. One problem programmers new to databases encounter is not understanding that databases are very, very good at this sort of thing. Don't be afraid to put your db to work. Commented Dec 8, 2015 at 22:09
  • 3
    Queries like 'find the Xth Song in the list' are no longer constant-time is also true for option 2. Commented Dec 8, 2015 at 22:17
  • 2
    Guys, the only thing I have against the first approach is that although it seems like it is cheap, because it only takes a couple of queries, it performs an update which modifies every row in the table, so it is an extremely expensive query. Commented Dec 8, 2015 at 22:21
  • 2
    @MikeNakis: It seems expensive, but all the work is being done on the server, which is (usually) optimized for this kind of work. I wouldn't use this technique on a table with millions of rows, but I wouldn't discount it for a table with only a couple thousand. Commented Dec 8, 2015 at 22:25