raphael232
Programmer
Hi, for a long time now i've been creating a different table for each section on my site (eg one for news and one for events etc). However i feel this adds alot of repeated work. I'm currently in the prcoess of combining them into a documents table. Here's what i have come up with so far:
Documents:
- ID (primary key)
- SectionID
- Title
... (other shared fields)
Sections:
- ID (primary key)
- SectionName
The problem i'm having is dealing with the extra fields that are unique to specific sections. My first thought was to create an additional table for each section with the extra fields, but this kinda is going back to what i started with. Here's the additional tables i come up with to combat this problem:
Attributes:
- ID (primary key)
- SectionID (section the attribute belongs too)
- FieldName
DocumentAttributes:
- ID (primary key)
- DocumentID (document attribute for)
- FieldID
- FieldValue
Which i thought was great at first but when i tried to implement and do a query to get the news i put:
SELECT dbo.Documents.* FROM dbo.Documents INNER JOIN DocumentFields ON Documents.ID = DocumentFields.DocumentID WHERE SectionID = 1
I expect you may have noticed the problem, the Documents table contains a single row for a record but the DocumenFields table contain multiple rows.
My next solution is to change my attributes table to:
- ID
- DocumentID
- AdditionalAttribute1
- AdditionalAttribute2
- AdditionalAttribute3
...
And join the document to the DocumentID and then i have my attributes. Now i need to create a new field for each attribute i wish to use. I'm not quite sure how effective this is because it would contain alot of redundant fields across all my sections of the site.
I'd appreciate peoples feedback to my problem and any solutions you may have as i'm sure i'm not the only person in the world who's encountered this problem before.
Thanks
Documents:
- ID (primary key)
- SectionID
- Title
... (other shared fields)
Sections:
- ID (primary key)
- SectionName
The problem i'm having is dealing with the extra fields that are unique to specific sections. My first thought was to create an additional table for each section with the extra fields, but this kinda is going back to what i started with. Here's the additional tables i come up with to combat this problem:
Attributes:
- ID (primary key)
- SectionID (section the attribute belongs too)
- FieldName
DocumentAttributes:
- ID (primary key)
- DocumentID (document attribute for)
- FieldID
- FieldValue
Which i thought was great at first but when i tried to implement and do a query to get the news i put:
SELECT dbo.Documents.* FROM dbo.Documents INNER JOIN DocumentFields ON Documents.ID = DocumentFields.DocumentID WHERE SectionID = 1
I expect you may have noticed the problem, the Documents table contains a single row for a record but the DocumenFields table contain multiple rows.
My next solution is to change my attributes table to:
- ID
- DocumentID
- AdditionalAttribute1
- AdditionalAttribute2
- AdditionalAttribute3
...
And join the document to the DocumentID and then i have my attributes. Now i need to create a new field for each attribute i wish to use. I'm not quite sure how effective this is because it would contain alot of redundant fields across all my sections of the site.
I'd appreciate peoples feedback to my problem and any solutions you may have as i'm sure i'm not the only person in the world who's encountered this problem before.
Thanks