Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 Design issues - dynamic templates

Author  Topic 

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2007-03-16 : 07:57:33
I'm currently working on a project that I'm having some design difficulties with and I was hoping that some of you could help me out. What I'm designing is sort of a webbased directory service based on different custom made templates. Imagine the yellow pages with different column requirements for each branch. Example:

Template: Restaurant
Columns: Name, Adress, TakeAway

Template: DBAForHire
Columns: Name, Adress, YearsExperience

I have designed a few tables that look like this:
DECLARE @templates TABLE 
(TemplateID INT IDENTITY(1, 1), TemplateName VARCHAR(50))
DECLARE @attributes TABLE
(AttributeID INT IDENTITY(1, 1), AttributeName VARCHAR(50))
DECLARE @template_attributes TABLE
(TemplateID INT, AttributeID INT)
DECLARE @values TABLE
(ValueID INT IDENTITY(1, 1), TemplateID INT, AttributeID INT, Value VARCHAR(200))

INSERT INTO @templates
SELECT 'Restaurant' UNION ALL SELECT 'DBAForHire'

INSERT INTO @attributes
SELECT 'Name' UNION ALL SELECT 'Adress' UNION ALL
SELECT 'TakeAway' UNION ALL SELECT 'YearsExperience'

INSERT INTO @template_attributes
SELECT 1, 1 UNION ALL SELECT 1, 2 UNION ALL SELECT 1, 3 UNION ALL
SELECT 2, 1 UNION ALL SELECT 2, 2 UNION ALL SELECT 2, 4

INSERT INTO @values
SELECT 1, 1, 'Lumbagos TexMex' UNION ALL SELECT 1, 2, 'Mainstreet Oslo' UNION ALL
SELECT 1, 3, 'Yes' UNION ALL SELECT 2, 1, 'Peso' UNION ALL
SELECT 2, 2, 'Somewhere IN Sweden' UNION ALL SELECT 2, 4, '10'

SELECT TemplateName, AttributeName, Value
FROM @templates a
INNER JOIN @template_attributes b
ON a.TemplateID = b.TemplateID
INNER JOIN @attributes c
ON b.AttributeID = c.AttributeID
INNER JOIN @values d
ON b.TemplateID = d.TemplateID AND c.AttributeID = d.AttributeID
The problem I need help with is that with the current design all "Values" are defined as varchar(200)...is it possible to change the design so that it's equally flexible but also fasilitates different datatypes? In some cases I would like to have a description column with way more than 200 characters, and I'd also like to save numbers as actual numbers...not like my example above with "YearsExperience". Is it at all possible?? I would appreciate your insight on this...

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-03-16 : 08:35:25
this looks like standard EAV model to me.

try this:
http://weblogs.sqlteam.com/mladenp/archive/2006/10/14/14032.aspx

and see if it works for you

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2007-03-16 : 09:27:39
Hm, I wasn't aware that this concept had a name and I'll have to read the article you linked a few times more since I'm not familiar at all with the xml datatypes and how they work in sql server. I did however find DavidM's blog about EAV (http://weblogs.sqlteam.com/davidm/articles/12117.aspx) which was very interesting and I do see a few more limitations (and possibilities!) after reading it.

I still belive that EAV is the right design for my app after reading the articles but I would be happy to see more comments about it from people that have used it before.

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-03-16 : 10:08:43
well eav with xml is very simple and pretty much quite easy.
personally i wouldn't do EAV in ss2k5 without xml.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-03-16 : 10:19:48
You could have several columns, one for each datatype, and a "definition" somewhere of which datatype a particular attribute [row] was.

Or you might use a sql_variant datatype.

Kristen

Go to Top of Page
   

- Advertisement -