对象所有者的架构问题
我在用户表中有一个 user_id 列。接下来我在品牌表中有一个brand_id。每个用例都有自己的页面:用户有个人资料,品牌有品牌页面。 (注意:这样的关系还有很多。公司有公司页面,城市有城市页面等)。因此,当用户上传照片时,我需要将其引用给所有者。所有者可以是品牌、用户、城市、组织等。
对于用户来说很简单,photo_id 属于 user_id,其中 user_id 是用户表的 FK。但是要将brand_id添加为照片的所有者,我是否需要一个单独的列,或者如果我将其重命名为将引用(用户ID或品牌ID)的owner_id,则可以在user_id列本身中完成吗?
I have a user_id colunm in the user table. Next I have a brand_id in the brand table. The usecase is each has their own page: The user has a profile and the brand has a brand page. (NOTE: Thgere are more relationships like this. A company has a company page, city has city page, etc). So when users upload photos I need to reference it to the owner. Owner can be brand, user, city, organization, etc.
For user's it is straightforward, photo_id belongs to user_id where user_id is a FK to user table. But to add the brand_id as the owner of the photo, do i require a seperate colunm or can it be done in the user_id colunm itself if i rename it as owner_id which will reference (userid or brand id)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
2 列,毫无疑问。 (当然,两者之一必须始终为空)
对于单个列,您会遇到许多问题:
2 columns, hands down. (One of the two must of course always be null)
With a single column you have a number of issues:
您可以为每个照片创建一个映射表,例如
user_photos
和brand_photos
。这样,您就可以拥有从
user_photos
到user
以及brand_photos
到brands
的完整引用完整性。这也对过滤用户/品牌照片的查询的性能产生积极影响。You could create one mapping table for each, such as
user_photos
andbrand_photos
.That way you have full referential integrity from
user_photos
touser
andbrand_photos
tobrands
. This also have a possitive impact on performance for queries that filters on user/brand photos.