Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > General > Database Concepts & Design > Intra-table relationships

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-05-08, 09:50
rapht rapht is offline
Registered User
 
Join Date: Jun 2008
Posts: 28
Intra-table relationships

Hi all,

I'm currently designing a database and am facing a question I couldn't relate to the traditional normal forms : what to do with intra-table relationships.
Let me explain.

I need to store items called "entities" which all share the same set of information : basically, a name and a type.

The thing is, these entities are related to each other by a parent-child relationship :
- I have one entity of type 1 that has no parent
- All entities of type 2 are children of the only entity of type 1
- All entities of type 3 have one entity of type 2 as parent
- All entities of type 4 have one entity of type 3 as parent
and so on.

The number of "types" is currently fixed but may change in the future, so it's best to consider it unknown.

The design I came up with is simply :
Code:
TABLE Entities (entity_id, entity_type, entity_parent)
where entity_parent is meant to hold a reference to an entity_id.

Is it acceptable as it is ? It sounds to me as far from best practices as one could be, but I could not think of something better - other than indeed creating as many tables as there are types right now and programmatically adding tables when new types are created in the future.

Thanks for any comments on that.
Reply With Quote
  #2 (permalink)  
Old 06-05-08, 10:18
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,539
Quote:
Originally Posted by rapht
Is it acceptable as it is ?
yes, it is
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #3 (permalink)  
Old 06-05-08, 10:21
pootle flump pootle flump is offline
COLOSSAL WIN
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 10,342
I'd say so but if you must enforce this type of thing relationally then perhaps:
TABLE Entities (entity_id, entity_type, entity_parent, entity_parent_type)
(i.e. two columns in the relationship) and a check constraint to enforce your rules.
Reply With Quote
  #4 (permalink)  
Old 06-05-08, 10:42
rapht rapht is offline
Registered User
 
Join Date: Jun 2008
Posts: 28
Quote:
Originally Posted by r937
yes, it is
Quote:
Originally Posted by pootle flump
I'd say so but if you must enforce this type of thing relationally then perhaps:
TABLE Entities (entity_id, entity_type, entity_parent, entity_parent_type)
(i.e. two columns in the relationship) and a check constraint to enforce your rules

Thanks for your help !
Reply With Quote
  #5 (permalink)  
Old 06-05-08, 11:31
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 9,274
Scrap the "Types" concept.
You can determine Type dynamically by the level of the record in the tree, and this way you will not need to "retype" branches if the structure of the tree changes.
__________________
If it's not practically useful, then it's practically useless.

blindman
http://sqlblindman.googlepages.com/main
Reply With Quote
  #6 (permalink)  
Old 06-05-08, 11:33
pootle flump pootle flump is offline
COLOSSAL WIN
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 10,342
Oh yes - that is much better.
Reply With Quote
  #7 (permalink)  
Old 06-05-08, 11:58
rapht rapht is offline
Registered User
 
Join Date: Jun 2008
Posts: 28
Quote:
Originally Posted by blindman
Scrap the "Types" concept.
You can determine Type dynamically by the level of the record in the tree, and this way you will not need to "retype" branches if the structure of the tree changes.
True. But :
- actually the tree should not change except for adding more depht.
- what's more important, I want to be able to easily get all the elements of one particular type/level in the tree. In your option, I would first need to recompile the tree to the given depht to get such a set.
Since such a query is going to be required frequently, I think I prefer the "typed" option, even though it's a bit less flexible.

Thanks anyway for the suggestion, will remember it for future use
Reply With Quote
  #8 (permalink)  
Old 06-05-08, 13:24
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 9,274
Quote:
Originally Posted by rapht
- actually the tree should not change except for adding more depht.
Should not and will not are miles apart. 95% of coding deals with issues that occur 5% of the time.

Quote:
Originally Posted by rapht
- what's more important, I want to be able to easily get all the elements of one particular type/level in the tree. In your option, I would first need to recompile the tree to the given depht to get such a set.
I'm not even sure what you mean by "recompile", so I doubt that whatever this action is would be necessary.
__________________
If it's not practically useful, then it's practically useless.

blindman
http://sqlblindman.googlepages.com/main
Reply With Quote
  #9 (permalink)  
Old 06-06-08, 08:02
rapht rapht is offline
Registered User
 
Join Date: Jun 2008
Posts: 28
Quote:
Originally Posted by blindman
I'm not even sure what you mean by "recompile", so I doubt that whatever this action is would be necessary.
Yeah that wasn't the right word, I meant "recalculate".
And as I said, if the tree "should not change", even if it does that will be kind of exceptionnal. That's why I think optimizing for reading/querying the tree by storing "types" is more relevant than optimizing for tree updating which will seldom happens.
But I wonder what's your take on that ?
Reply With Quote
  #10 (permalink)  
Old 06-06-08, 11:30
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 9,274
I already told you my take on that.
You have a business rule defining the labels for different levels of the tree. Rather than calculating this dynamically, you are going to store it in the tree.
What are you going to add to the database to ensure that the labels are correct? What constraints or triggers will you write to enforce the data integrity?
Any code you add will have its own overhead, in CPUs and administrative maintenance. And if you do not enforce these constraints by some means, then they are not constraints are they? And sooner or later they WILL be violated, leaving you with corrupted data and incorrect output.
How many records do you anticipate having in this table?
__________________
If it's not practically useful, then it's practically useless.

blindman
http://sqlblindman.googlepages.com/main
Reply With Quote
  #11 (permalink)  
Old 06-06-08, 11:40
pootle flump pootle flump is offline
COLOSSAL WIN
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 10,342
Quote:
Originally Posted by blindman
What constraints or triggers will you write to enforce the data integrity?
Any code you add will have its own overhead, in CPUs and administrative maintenance.
Post #3 - declarative integrity - overhead nominal - no triggers.
Reply With Quote
  #12 (permalink)  
Old 06-06-08, 13:08
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 9,274
How would that handle updates to EntityType? I think every attempt would fail until you removed the constraint.
__________________
If it's not practically useful, then it's practically useless.

blindman
http://sqlblindman.googlepages.com/main
Reply With Quote
  #13 (permalink)  
Old 06-07-08, 06:53
pootle flump pootle flump is offline
COLOSSAL WIN
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 10,342
The entity type relationship between parent and child is relative so there is no need for literals in the constraint (other than null parent if entity type = 1).
Reply With Quote
  #14 (permalink)  
Old 06-07-08, 11:49
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 9,274
But if you change a parenttype in a record, will that change cascade to the parenttype in all the related records?
__________________
If it's not practically useful, then it's practically useless.

blindman
http://sqlblindman.googlepages.com/main
Reply With Quote
  #15 (permalink)  
Old 06-07-08, 11:54
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 9,274
Say you have this data:
Code:
ID Type ParentID ParentType 1 A 2 B 1 A 3 C 2 B
All well and good, because you have a business rule that 1st level entities are type "A", second level are type "B" and third level are type "C", etc, and this data satisfies the constraints you outlined above.
Now, you promot record 2 to the top level, changing its type from "B" to "A". Maybe you can get ParentType of record 3 to update from "B" to "A" by cascading the change, but you are still going to need a trigger or something to update record 3's type from "C" to "B".
So you may get this to work, but I do not see it as a simple solution.
__________________
If it's not practically useful, then it's practically useless.

blindman
http://sqlblindman.googlepages.com/main
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On