Unix Technical Forum

ordered tables

This is a discussion on ordered tables within the MySQL forums, part of the Database Server Software category; --> subtenante wrote: > On Fri, 15 Jun 2007 00:41:47 -0700, strawberry <zac.carey@gmail.com> > wrote: > >> Perhaps you should ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 02-28-2008, 10:02 AM
Paul Lautman
 
Posts: n/a
Default Re: ordered tables

subtenante wrote:
> On Fri, 15 Jun 2007 00:41:47 -0700, strawberry <zac.carey@gmail.com>
> wrote:
>
>> Perhaps you should take a look at nested sets:
>>
>> http://dev.mysql.com/tech-resources/...ical-data.html
>>
>> There's a good article at sitepoint too

>
> This is a good thing for trees but for linked list, i'm not sure it is
> of great help, it would result in the solution Jon was running away
> from (updating in mean half of the rows for each insert/delete).


But Jon has said:
"100's of nodes. its (SIC) about nesting or if you prefer, tree's."


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 02-28-2008, 10:02 AM
subtenante
 
Posts: n/a
Default Re: ordered tables

On Fri, 15 Jun 2007 14:37:40 +0100, "Paul Lautman"
<paul.lautman@btinternet.com> wrote:

>> This is a good thing for trees but for linked list, i'm not sure it is
>> of great help, it would result in the solution Jon was running away
>> from (updating in mean half of the rows for each insert/delete).

>
>But Jon has said:
>"100's of nodes. its (SIC) about nesting or if you prefer, tree's."


True, but if you want to order a table, you have more precisely a
linked list shape (which is a tree, except it has only one branch).
And in that case, using the nested sets pattern makes it quite absurd
: each set contains only one set containing one set containing one
set... therefore the left column lists your elements in one sense, and
the right column lists them backwards. So it provides no advantage :
for each insert/delete, you have to update two values in each row of
half of your columns, instead of updating only one with the initial
shape Jon had in the first place (which is only the equivalent of the
left column in nested sets).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 02-28-2008, 10:02 AM
subtenante
 
Posts: n/a
Default Re: ordered tables

On Fri, 15 Jun 2007 22:47:28 +0800, subtenante
<zzsubtenantezz@gmail.com> wrote:

Sorry got mixed up ! I correct :

So it provides no advantage :
for each insert/delete, you have to update two values in half of your
ROWS (in mean)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 02-28-2008, 10:02 AM
Jon Slaughter
 
Posts: n/a
Default Re: ordered tables


"Paul Lautman" <paul.lautman@btinternet.com> wrote in message
news:5dfj0nF34akieU1@mid.individual.net...
> subtenante wrote:
>> On Fri, 15 Jun 2007 00:41:47 -0700, strawberry <zac.carey@gmail.com>
>> wrote:
>>
>>> Perhaps you should take a look at nested sets:
>>>
>>> http://dev.mysql.com/tech-resources/...ical-data.html
>>>
>>> There's a good article at sitepoint too

>>
>> This is a good thing for trees but for linked list, i'm not sure it is
>> of great help, it would result in the solution Jon was running away
>> from (updating in mean half of the rows for each insert/delete).

>
> But Jon has said:
> "100's of nodes. its (SIC) about nesting or if you prefer, tree's."
>


Yes I did.. but nesting can be many things and there are many types of
tree's. subtenante was right that I want to avoid having to update all the
rows for each insert. Since there are no deletes I think a linked list like
method is probably the best but unfortunately I really need a multilinked
list...

Since there can only be one parent and multiple children I think I can use a
variant of the linked list version he suggested. Just keep all the
children's id's in the parent and update only the parent when a new child is
added. Since all the children are specificed in the parent I should easily
be able to traverse through the tree in the manner I wanted.

Here the order by sql won't matter because the order is contained in the
parents field that contains all the children's id's. So this is another
plus.

So far this method seems to be the best and does what I want.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 02-28-2008, 10:02 AM
Jon Slaughter
 
Posts: n/a
Default Re: ordered tables


"subtenante" <zzsubtenantezz@gmail.com> wrote in message
news:4oa473h3gmkkmdb43kpg50v4pr0p59k8ek@4ax.com...
> On Thu, 14 Jun 2007 15:35:05 -0500, "Jon Slaughter"
> <Jon_Slaughter@Hotmail.com> wrote:
>
>>Is there such a thing as ordered tables in sql or a simple way to emulate
>>it? i.e., the order in the table matters and one can insert into into the
>>table w.r.t to this order?
>>
>>I suppose I can have a column that sorta simulates the order but the
>>problem
>>is that I don't see how I can simply insert a new row without having to
>>modify all others. If I could simply use the inherent order of the table
>>then it would be much easier but then I think inserting will be very hard.
>>I
>>suppose I could have a unique ID for each one and I think that will work
>>but
>>not completely sure.
>>
>>Not sure if its possible to insert a row inbetween rows? But if I could
>>and
>>I could be sure that SQL would not modify the order then I think I could
>>do
>>it easily... else I end up having to have a field that contains relative
>>information and I'd have to check every single row for it which is not
>>what
>>I'm going to do.
>>
>>Any ideas?
>>
>>Thanks,
>>Jon
>>

>
> I would do something dirty like this :
> table{id, parentid, childid, order, ...}
>
> order is a BIGINT, generated by a procedure. It starts from the row
> with parentid = null, puts order = 1000. (pretending you don't plan to
> have more than the max value of a BIGINT/1000 rows in your table)
> You affect an order of n+1000 for the child of the row that had n as
> order.
>
> When you insert a row, you must update : the child value of its parent
> and parent value of it child, and give the new row an order of
> (parentorder+childorder)/2. That's done with a procedure but it's not
> too heavy and has to modify only two rows.
> If it is not possible to get a proper order (neworder=parentorder OR
> neworder = childorder), call the procedure that reaffects all the
> orders (which will be done only when you have inserted 1000 rows
> between two original rows).


Ok, I think I will use a variant of this. childid will actually be a
contatenation of all the child id's so that I can have multipl children per
parent. I don't think this significantly changes anything you said though.
All I have to do is update the parent by appending the new child id to the
childid's in the parent... I don't even need a procedure.

make sense?

Thanks,
Jon


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 02-28-2008, 10:02 AM
Carlos Troncoso
 
Posts: n/a
Default Re: ordered tables

On 15 jun, 14:21, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com> wrote:
> "subtenante" <zzsubtenant...@gmail.com> wrote in message
>
> news:4oa473h3gmkkmdb43kpg50v4pr0p59k8ek@4ax.com...
>
>
>
> > On Thu, 14 Jun 2007 15:35:05 -0500, "Jon Slaughter"
> > <Jon_Slaugh...@Hotmail.com> wrote:

>
> >>Is there such a thing as ordered tables in sql or a simple way to emulate
> >>it? i.e., the order in the table matters and one can insert into into the
> >>table w.r.t to this order?

>
> >>I suppose I can have a column that sorta simulates the order but the
> >>problem
> >>is that I don't see how I can simply insert a new row without having to
> >>modify all others. If I could simply use the inherent order of the table
> >>then it would be much easier but then I think inserting will be very hard.
> >>I
> >>suppose I could have a unique ID for each one and I think that will work
> >>but
> >>not completely sure.

>
> >>Not sure if its possible to insert a row inbetween rows? But if I could
> >>and
> >>I could be sure that SQL would not modify the order then I think I could
> >>do
> >>it easily... else I end up having to have a field that contains relative
> >>information and I'd have to check every single row for it which is not
> >>what
> >>I'm going to do.

>
> >>Any ideas?

>
> >>Thanks,
> >>Jon

>
> > I would do something dirty like this :
> > table{id, parentid, childid, order, ...}

>
> > order is a BIGINT, generated by a procedure. It starts from the row
> > with parentid = null, puts order = 1000. (pretending you don't plan to
> > have more than the max value of a BIGINT/1000 rows in your table)
> > You affect an order of n+1000 for the child of the row that had n as
> > order.

>
> > When you insert a row, you must update : the child value of its parent
> > and parent value of it child, and give the new row an order of
> > (parentorder+childorder)/2. That's done with a procedure but it's not
> > too heavy and has to modify only two rows.
> > If it is not possible to get a proper order (neworder=parentorder OR
> > neworder = childorder), call the procedure that reaffects all the
> > orders (which will be done only when you have inserted 1000 rows
> > between two original rows).

>
> Ok, I think I will use a variant of this. childid will actually be a
> contatenation of all the child id's so that I can have multipl children per
> parent. I don't think this significantly changes anything you said though.
> All I have to do is update the parent by appending the new child id to the
> childid's in the parent... I don't even need a procedure.
>
> make sense?
>
> Thanks,
> Jon


I've done that sort of ordering. I like tu use mysql to store a kind
of scripting procedures that must be processed in order.
Such as "sum of sales of 2 accounts A and B in account C, delete
Account A, Delete account B.... etc"
As you can see, this are 3 rows in my database.. that a program reads
sequentialy and executes a procedure based on what the record says.
It obvious that if it's not processed in order.. the result is
different or just ends up crashing.
So, I looked back to the old days of BASIC.. yes.. the famous Biginers
All-purpose Symbolic Instruction Code.(not VB)
On that days, programs used to have line numbers... such as
10 Let a="Hello world"
20 Print a
30 goto 20

as you can see, lines where numbered 10 by 10.. so if you needed to
insert lines in the middle, you just add line 15 Let a= a +"!!!!!" to
end up with
10 Let a="Hello world"
15 Let a= a +"!!!!!"
20 Print a
30 goto 20

Well, it was quite easy to end up without space.. so just type
RENUMBER (was it renumber?).. and presto!
10 Let a="Hello world"
20 Let a= a +"!!!!!"
30 Print a
40 goto 30

So, I decided to follow the idea.
All I needed was a renumbering procedure... which I won't type now
because It's 3 in the morning and I'm quite exhausted.. and btw,
you'll get the idea.
what you need is a table with a LINE NUMBER COLUMN.
The increment on the line number, I like it to be a power of 2, as
2,4,8 16,32, 64 and so on has always an integer in it's middle where
to insert. Use a trigger to update that column, not an autoincrement.
Well, let's say that you added stuff to the database, it looks neat..
but wait!.. I need to insert a few row in the middle.. well. depending
on the increment you used, you got space...
But hay.. what if I ran out of space?
Well, renumber the rows.
Renumbering is done with a procedure, that counts the rows, multiplies
it by the increment... (let's say 32) to get the largest line number,
and using a cursor with a select ordered by linenumber DECRementing (<-
VERY IMPORTANT!!!) . I update the last row with that value, subtract
32, fetch, update, subtract, fetch... and so on.
Result, linenumber renumbered and you got your space back.
It's important to do it backwards because if you do it forewards, you
might be stepping over other record's line number and during the
procedure, you end up with 2 records with linenumber, let's say, 64.
So when we get to line 64, what record need's it's line updated?... If
you got the idea, great, if you don't.. trust me.. don't re-invent the
wheel.

Hope it helped you out.
Carlos Troncoso Phillips


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 02-28-2008, 10:03 AM
subtenante
 
Posts: n/a
Default Re: ordered tables

On Fri, 15 Jun 2007 18:21:00 GMT, "Jon Slaughter"
<Jon_Slaughter@Hotmail.com> wrote:

>Ok, I think I will use a variant of this. childid will actually be a
>contatenation of all the child id's so that I can have multipl children per
>parent. I don't think this significantly changes anything you said though.
>All I have to do is update the parent by appending the new child id to the
>childid's in the parent... I don't even need a procedure.
>
>make sense?


Ok, sorry i didn't understand you had really a tree structure, I
thought from your first post it was a total order, so Captain was
right in his remark higher.

Anyways i think having a concatenated childid column is a bad id. You
can have all the children with a
SELECT * FROM table WHERE parentid=...
The reason I put it in the first place was only convenience, but now
you have the inconvenient of needing to parse your childid column. And
personnally I would say : don't pretend you wil not delete. Eventually
you will (because of mistakes, or anything). So keep your things
normalized as much as you can : you may have a hard time removing
values inside of concatenated fields (I don't mind doing a dirty thing
from time to time, assuming certain things that are really unlikely to
be overruled, but this is *really* dirty). But i don't know everything
of the case, so if you have really good reasons to do so, do so. But
find out your good reasons before.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18 (permalink)  
Old 02-28-2008, 10:03 AM
subtenante
 
Posts: n/a
Default Re: ordered tables

On Sat, 16 Jun 2007 07:33:06 -0000, Carlos Troncoso
<schmickcl@gmail.com> wrote:

>The increment on the line number, I like it to be a power of 2, as
>2,4,8 16,32, 64 and so on has always an integer in it's middle where
>to insert.


This power-of-2 thing is very smart !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:34 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com