How to structure table Activities in a database?

青春壹個敷衍的年華 提交于 2019-12-25 04:06:08

问题


I have a site written in cakephp with a mysql database. Into my site I want to track the activities of every users, for example (like this site) if a user insert a product I want to put this activity into my database. I have 2 ways: 1) One table called Activities with: - id - user_id - title - text - type (the type of activity: comment, post edit)

2) more table differenced by activities - table activities_comment - table activities_post - table activities_badges

The problem is when I go to the page activities of a user I can have different type of activities and I don't know which of this solution is better because a comment has a title and a comment, a post has only a text, a badge has an external id to its table (for example) ecc... Help me please


回答1:


I'm not familiar with CakePHP, but from purely database perspective your data model should probably look similar to this:

The

symbol denotes category (aka. inheritance, subclass, subtype, generalization hierarchy etc.). Take a look at "Subtype Relationships" in ERwin Methods Guide for more info.

There are generally 3 strategies for implementing the category:

  1. All types in single table. This requires a lot of NULLs and requires CHECKs to make sure separate subtypes are not inappropriately "intermingled".
  2. All concrete types in separate tables (excluding the base, which is ACTIVITY in your case), which means common fields and relationships must be repeated in all child tables.
  3. All types in separate tables (including the base). This implementation requires a little more JOINing, but is flexible and clean. It should be your default, unless there are strong reasons against it.


来源:https://stackoverflow.com/questions/11461075/how-to-structure-table-activities-in-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!