开发者

Best way to design this database scenario?

开发者 https://www.devze.com 2023-01-10 10:24 出处:网络
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).

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

Attchment开发者_开发百科Type 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?


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?


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.


Hope this is self-explanatory.

Best way to design this database scenario?


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)


I would go with option 2.

Something like this:

alt text http://img84.imageshack.us/img84/815/dbso.png


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号