回答

收藏

数据库设计-可为空的字段

技术问答 技术问答 365 人阅读 | 0 人回复 | 2023-09-12

因为我是数据库设计的新手,所以有一个“最佳实践”问题,我想确保自己在正确的方向上工作
* |6 d) u; ^7 b我有三种用户类型:用户(单人)、组(许多用户)和公司(许多组)。每个用户都有自己的登录名称,允许他们发布消息。因此,例如。如果公司发布消息,它将显示在所有链接的用户新闻摘要中。
* ~8 v' ]( j+ s为此,我有一个表  messages存储信息内容和链接用户类型的外键- a* R4 \) W9 F9 N$ @) ~) e
我打算使用以下架构(PostgreSQL)实现这一目标…8 c. k6 p# r& v1 e( g
create table notifications(    notification_id serial primary key,   user_id integer references users,   group_id integer references groups,   company_id integer references companies,   date_created timestamp not null default now(),   title_id text not null,   message_id text not null,   icon text not null default 'logo');comment on table notifications is 'Messages to be displayed on a users home feed';这将允许我构建一个查询来提取用户新闻提取的相关信息(例如,只有一个字段user_id,group_id或company_id将具有值)
# r; t9 H9 w7 l% R, A  b, C但这是最好的方法吗?我一定有一个空的外键是个坏主意。我认为使用枚举键可能会有更好的解决方案?(这是否存在?!); Y  u5 }' X! G2 n8 R5 Y7 N7 F6 [
谢谢" r8 s* _# {6 _$ N# `0 E# ?
                                                                9 T2 d8 I0 K7 ?3 ^, s
    解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则