首页 > 解决方案 > 您会在父表中添加 owner_id 还是在子表中添加 is_owner?

问题描述

假设你有一张桌子

collection
    id
    name

和一张桌子

collection_collaborator
    id
    user_id

您会在父表中添加 owner_id 吗?

collection
    id
    name
    owner_id

你会在子表上添加一个 is_owner 吗?

collection_collaborator
    id
    name
    is_owner

标签: mysqldatabasedatabase-design

解决方案


这个问题没有非黑即白的答案——实际上取决于您的业务需求和您自己的偏好。

这两种方法有一个明显的区别:owner_id字段表示集合只能有一个所有者,而is_owner标志允许单个集合有多个所有者。

The other deciding factor could be the reports on the data. If you have owner_id field, then you can display the owner information for a collection without needing to join on the collaborator table. If you have lots of reports where you need the owner information, but do not need any other information around the collaborators, then owner_id field can increase the performance of your queries.


推荐阅读