问题
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

There are generally 3 strategies for implementing the category:
- All types in single table. This requires a lot of NULLs and requires CHECKs to make sure separate subtypes are not inappropriately "intermingled".
- 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.
- 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