views:

102

answers:

5

Requirement is to store attachments for different entity types.

Say we have 3 entity types Company , Department and Employee. Each can have multiple attachments (documents).

Which is the best way to handle this?

Solution 1:

Company table

  • CompanyId

Dept table

  • DeptId

Employee table

  • EmployeeId

AttchmentType table

  • TypeId
  • Types (company, dept, employee)

Attachments table

  • AttachmentId
  • TypeId (maps to attachment type)
  • entityId (maps to CompanyId / DeptId / EmployeeId)

Pros: I can add new entity types easily in future

Cons: In this case I can't have foreign key relationship maintained between entities and attachments.

Solution 2:

Company table

  • CompanyId

Dept table

  • DeptId

Employee table

  • EmployeeId

CompanyAttachments table

  • AttachmentId
  • CompanyId (FK)

DeptAttachments table

  • AttachmentId
  • DeptId (FK)

EmployeeAttachments table

  • AttachmentId
  • EmployeeId (FK)

Pros: Foreign key integrity

Cons: In order add new entity I need to have new attachment table separately.

So which is the best way to go with assuming I may need to add new entities in future? Please help and thanks in advance!


Edit 1:

Thanks for your reply guys.

If I want to go with solution 2, I see that creating new columns in attachments table easier instead of creating new attachment tables for every entity just to map them? something like,

Company table

  • CompanyId

Dept table

  • DeptId

Employee table

  • EmployeeId

Attachments

  • AttachmentId
  • CompanyId (FK)
  • EmployeeId (FK)
  • DepartmentId (FK)

am I missing something here?

+3  A: 

I vote for solution 2 because this way you can enforce referential integrity in a proper way. In addition you can easily (if needed) add fields for special attachments (for instance EmployeeAttachments might have a bit field "PersonalPicture" or similar)

Vidar Nordnes
+1, `add fields for special attachments` users will eventually want this.
KM
+4  A: 

I'd definitely go with solution #2. Your one pro for solution #1 isn't really a pro. If you add a new entity you're going to necessarily have to already add a new table for that entity and you'll already be adding or changing existing code to handle it. You should be able to make some generic objects that handle the pattern so that duplicated code isn't a problem.

Tom H.
Data integrity enforced through FKs beats less time for maintenance (especially a type of maintenance that may never be needed) every time!
HLGEM
+1, solution #1 could have probelms, what happens when you have the same key value in CompanyId / DeptId / EmployeeId, like if you use identity / auto increment values.
KM
+2  A: 

I would go with option 2.

Something like this:

alt text

Martin
I like this solution, given that attachments table is going to have same properties for all entities.
puzzled
@puzzled Fields that are not common will go to their specialized tables
Tassadaque
A: 

Other things to consider:

Are you going to need to roll up attachments? i.e. an employee's attachments are associated with his/her department and their company? if this is a frequent query a single attachment table and a separate and heavily index entity look-up table option may give better query performance.

Also, Are the attachments going to be many and/or huge enough that you need to put those table(s) on a separate device, or another storage system (i.e. file system pointers)? Management is an issue as well as performance.

Rawheiser
Nope. Attachments are just related to their corresponding entity and are not rolled up.Attachments table just stores the filepath/url. Actual files are stored on fileserver.
puzzled
Okay then, separate tables are the way to go.I do think you aught to have a union view of all the file names or paths, so that you can audit the data. i.e. did someone accidentally delete the file, etc... Also if archival is needed, think about how you would handle zipping contents and moving to difference devices (i.e. from one disk to the other off to DVD, etc..)
Rawheiser
+1  A: 

alt text

Damir Sudarevic
Thanks Damir. I hope this must be the optimal solution, if we start from scratch. But unfortunately in my case, the entities are already in use, with their own Ids in place.
puzzled