dbTalk Databases Forums  

Managing a Many-to-Many Relationship Tree by tracking one-many relations

comp.databases comp.databases


Discuss Managing a Many-to-Many Relationship Tree by tracking one-many relations in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
isporter@gmail.com
 
Posts: n/a

Default Managing a Many-to-Many Relationship Tree by tracking one-many relations - 09-29-2006 , 06:05 AM






I have Tags and Articles in a many-to-many relationship - Tags form a
taxonomy for Articles. This taxonomy is then used to build the
navigation of a site displaying the information in Articles. Thus Tags
need to be given relations - a Tag can have many child Tags, and a
child Tag can have many parent Tags. For example, the Tag 'USA' might
have the child 'News', but that Tag also has the parent 'Australia'.

I began with an Articles table, a Tags table, a Tags_Articles_Relations
table, and a Tags_Relations table. The first Relations table allows
for the many-to-many relationship between articles and tags. The
second Relations table had Tag_ID and Parent_ID as foreign keys to ID
in the Tag table. This table made a composite PK from Tag_ID,
Parent_ID, & Rank, allowing a Tag to have a different rank under
different parents.

However, this causes problems for querying - to know the page the user
is on, I would need to always know the complete path of Tag IDs to the
tree's root.

Can I, instead, give each relationship (Tags_Relations tuple) a PK
attribute 'ID'? Note that each tree branch (Relation) now has a unique
ID, and a one-many relationship with child branches, meaning that
instead of having to know the complete path to the tree's root, I just
need to know the ID of the relation between the current Tag and it's
parent, and the Relation ID of the parent Relation of that relation.

Thus, if I add a further table 'Relations_Relations', with FKs
Relation_ID and Parent_ID, I will be able to use this to traverse the
tree?

Is this a solid database design? Explanatory diagrams below:
http://www.intraspin.com/tree.gif
http://www.intraspin.com/DBdiagram.gif

Many thanks,
Iain


Reply With Quote
  #2  
Old   
Bob Stearns
 
Posts: n/a

Default Re: Managing a Many-to-Many Relationship Tree by tracking one-manyrelations - 09-29-2006 , 09:09 AM






isporter (AT) gmail (DOT) com wrote:
Quote:
I have Tags and Articles in a many-to-many relationship - Tags form a
taxonomy for Articles. This taxonomy is then used to build the
navigation of a site displaying the information in Articles. Thus Tags
need to be given relations - a Tag can have many child Tags, and a
child Tag can have many parent Tags. For example, the Tag 'USA' might
have the child 'News', but that Tag also has the parent 'Australia'.

I began with an Articles table, a Tags table, a Tags_Articles_Relations
table, and a Tags_Relations table. The first Relations table allows
for the many-to-many relationship between articles and tags. The
second Relations table had Tag_ID and Parent_ID as foreign keys to ID
in the Tag table. This table made a composite PK from Tag_ID,
Parent_ID, & Rank, allowing a Tag to have a different rank under
different parents.

However, this causes problems for querying - to know the page the user
is on, I would need to always know the complete path of Tag IDs to the
tree's root.

Can I, instead, give each relationship (Tags_Relations tuple) a PK
attribute 'ID'? Note that each tree branch (Relation) now has a unique
ID, and a one-many relationship with child branches, meaning that
instead of having to know the complete path to the tree's root, I just
need to know the ID of the relation between the current Tag and it's
parent, and the Relation ID of the parent Relation of that relation.

Thus, if I add a further table 'Relations_Relations', with FKs
Relation_ID and Parent_ID, I will be able to use this to traverse the
tree?

Is this a solid database design? Explanatory diagrams below:
http://www.intraspin.com/tree.gif
http://www.intraspin.com/DBdiagram.gif

Many thanks,
Iain

Is the set of articles USA->News disjoint from the set Australia->News?
Do you ever want the set of News articles regardless of parentage?
If you answer yes to the first and no to the second question, then you
have a real tree and you need to get and implement Joe Celko's "Trees
and Hierarchies in SQL for Smarties". Otherwise, what you have is a
general directed graph, which has (as far as I know) no good, general
representation in SQL.



Reply With Quote
  #3  
Old   
isporter@gmail.com
 
Posts: n/a

Default Re: Managing a Many-to-Many Relationship Tree by tracking one-many relations - 09-30-2006 , 04:43 AM



If my understanding is corrent, the article sets are not disjoint
because articles may be tagged with both Australia and USA.

I don't believe I will ever want to display articles in a manner not
based on their tag metadata.

Say I have a root of 1, with children 2, 3, 4. Both children 2 and 3
can have the child 5.

You are suggesting that there is no good general representation in SQL
for this? What should I be considering to accomplish this data
structure?

Thanks,
Iain

Bob Stearns wrote:
Quote:
isporter (AT) gmail (DOT) com wrote:
I have Tags and Articles in a many-to-many relationship - Tags form a
taxonomy for Articles. This taxonomy is then used to build the
navigation of a site displaying the information in Articles. Thus Tags
need to be given relations - a Tag can have many child Tags, and a
child Tag can have many parent Tags. For example, the Tag 'USA' might
have the child 'News', but that Tag also has the parent 'Australia'.

I began with an Articles table, a Tags table, a Tags_Articles_Relations
table, and a Tags_Relations table. The first Relations table allows
for the many-to-many relationship between articles and tags. The
second Relations table had Tag_ID and Parent_ID as foreign keys to ID
in the Tag table. This table made a composite PK from Tag_ID,
Parent_ID, & Rank, allowing a Tag to have a different rank under
different parents.

However, this causes problems for querying - to know the page the user
is on, I would need to always know the complete path of Tag IDs to the
tree's root.

Can I, instead, give each relationship (Tags_Relations tuple) a PK
attribute 'ID'? Note that each tree branch (Relation) now has a unique
ID, and a one-many relationship with child branches, meaning that
instead of having to know the complete path to the tree's root, I just
need to know the ID of the relation between the current Tag and it's
parent, and the Relation ID of the parent Relation of that relation.

Thus, if I add a further table 'Relations_Relations', with FKs
Relation_ID and Parent_ID, I will be able to use this to traverse the
tree?

Is this a solid database design? Explanatory diagrams below:
http://www.intraspin.com/tree.gif
http://www.intraspin.com/DBdiagram.gif

Many thanks,
Iain

Is the set of articles USA->News disjoint from the set Australia->News?
Do you ever want the set of News articles regardless of parentage?
If you answer yes to the first and no to the second question, then you
have a real tree and you need to get and implement Joe Celko's "Trees
and Hierarchies in SQL for Smarties". Otherwise, what you have is a
general directed graph, which has (as far as I know) no good, general
representation in SQL.


Reply With Quote
  #4  
Old   
Bob Stearns
 
Posts: n/a

Default Re: Managing a Many-to-Many Relationship Tree by tracking one-manyrelations - 09-30-2006 , 01:03 PM



isporter (AT) gmail (DOT) com wrote:
Quote:
If my understanding is corrent, the article sets are not disjoint
because articles may be tagged with both Australia and USA.

I don't believe I will ever want to display articles in a manner not
based on their tag metadata.

Say I have a root of 1, with children 2, 3, 4. Both children 2 and 3
can have the child 5.

You are suggesting that there is no good general representation in SQL
for this? What should I be considering to accomplish this data
structure?

Thanks,
Iain

Bob Stearns wrote:

isporter (AT) gmail (DOT) com wrote:

I have Tags and Articles in a many-to-many relationship - Tags form a
taxonomy for Articles. This taxonomy is then used to build the
navigation of a site displaying the information in Articles. Thus Tags
need to be given relations - a Tag can have many child Tags, and a
child Tag can have many parent Tags. For example, the Tag 'USA' might
have the child 'News', but that Tag also has the parent 'Australia'.

I began with an Articles table, a Tags table, a Tags_Articles_Relations
table, and a Tags_Relations table. The first Relations table allows
for the many-to-many relationship between articles and tags. The
second Relations table had Tag_ID and Parent_ID as foreign keys to ID
in the Tag table. This table made a composite PK from Tag_ID,
Parent_ID, & Rank, allowing a Tag to have a different rank under
different parents.

However, this causes problems for querying - to know the page the user
is on, I would need to always know the complete path of Tag IDs to the
tree's root.

Can I, instead, give each relationship (Tags_Relations tuple) a PK
attribute 'ID'? Note that each tree branch (Relation) now has a unique
ID, and a one-many relationship with child branches, meaning that
instead of having to know the complete path to the tree's root, I just
need to know the ID of the relation between the current Tag and it's
parent, and the Relation ID of the parent Relation of that relation.

Thus, if I add a further table 'Relations_Relations', with FKs
Relation_ID and Parent_ID, I will be able to use this to traverse the
tree?

Is this a solid database design? Explanatory diagrams below:
http://www.intraspin.com/tree.gif
http://www.intraspin.com/DBdiagram.gif

Many thanks,
Iain


Is the set of articles USA->News disjoint from the set Australia->News?
Do you ever want the set of News articles regardless of parentage?
If you answer yes to the first and no to the second question, then you
have a real tree and you need to get and implement Joe Celko's "Trees
and Hierarchies in SQL for Smarties". Otherwise, what you have is a
general directed graph, which has (as far as I know) no good, general
representation in SQL.


Joe Celko is working on a book on representing this form of general
directed graph, or at least that is what I gathered from some items in
the newsgroups from him about two months ago. I certainly am not current
on research in this area, so maybe Google searching on "directed graphs"
and sql might be of some benefit.

BTW, if your taxonomy is not too deep, you might consider a table with
all the taxonomy levels and the article id (with the whole row as
primary key). This is not scalable wrt taxonomy or good sql practice (it
violates both the "array" proscription and the redundancy proscription),
but if your taxonomy is reasonably static and you have some type of
deadline, then it is a workable option.


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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.