Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Tip: Easy schema to store ANYTHING.
Old 08-21-2007, 06:56 PM Tip: Easy schema to store ANYTHING.
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
A friend has been asking me how to model some very complex data that's only somewhat relational, in a sql database. He's got some application writing and designing skills, but no database knowledge whatsoever.

Anyway, I thought the answer we came up with might be of use to others. This really isn't a new concept, but it's not a very well known one ... in other words I didn't invent this schema, I'm just pointing it out in case this can help anybody model very complex data. This is optimized for flexibility, not speed.

The code below is for sql server 2005, but should be very easy to port to whatever database engine you use.

Code:
Create Table XmlClobs (
   XmlDocument Text,
   HashValue varBinary(36)
   XmlID int identity primary key,
)

Create Table Objects (
   Label varChar(200) not null,
   ObjectID int identity primary key
)

Create Table Attributes (
   AttribName varChar(100) not null,
   ShortValue varChar(300),
   XmlID int foreign key references XmlClobs,
   AttributeID int primary key
)

Create Table ObjectAttributes (
   ObjectID not null int foreign key references Objects,
   AttributeID not null int foreign key references Attributes,
   ObjAtLinkID int identity primary key
)

Create Table Concepts (
   Label varChar(100) not null,
   Description varChar(300),
   ConceptID int identity primary key
)

Create Table ConceptMapping (
   ConceptID not null foreign key references Concepts,
   ObjectID foreign key references Objects,
   AttributeID foreign key references Attributes,
   XmlID foreign key references XmlClobs
)
You can actually get away with just two tables: Objects, and Attributes, but a third table to map these together lets you reuse the same attribute between several objects.

The concepts table isn't necessary, but gives you the ability to provide something like folders in your application. An "object" can be anything - an email thread, a powerpoint slide, a goal for your web site, the content for an article ... so putting these broad categories in a concepts table lets you query specifically for what you want without having to wade for it.

An attribute can be a "name-value pair" like a registry key or a definition list in html. That's more efficient. But, if an object has 200 attributes you want to track, you can store all of this in Xml instead ... XmlClob is a really bad name, but XmlData was taken. You can change this to suit our purposes.

Hope this helps.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
 
Register now for full access!
Old 08-21-2007, 07:52 PM Re: Tip: Easy schema to store ANYTHING.
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Way cool! Honestly, this is news to me. I haven't seen anything like this but it's one of those things that's beautiful in it's simplicity. Good work!

How do you interact with this kind of database from your application code?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Tip: Easy schema to store ANYTHING.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28299 seconds with 12 queries