【发布时间】:2026-02-07 17:15:02
【问题描述】:
在 postgres 9.3 中存储少量关系数据的最佳方式是什么?
- 例如每个用户的角色(编辑、发布者、管理员、查看者、 贡献者)
- 例如应用程序可读的文件扩展名(pdf、txt、epub、 rtf)
通常情况下,这会像...
CREATE TABLE users (
id SERIAL,
...
);
CREATE TABLE roles (
id SERIAL,
name VARCHAR(50), -- editor, publisher ...
description VARCHAR(50) -- ideally, UI display would be different than database codename, e.g. "Administrator" for "admin"
);
CREATE TABLE user_roles (
user_id int,
role_id int,
...
PRIMARY KEY (user_id, role_id)
);
要求是
- en 实体(例如用户)可以分配 0、1 或 N 个项目(例如角色),其中 N
- 数据必须能够快速过滤(例如,SELECT all users with role=publisher)
考虑到 N
【问题讨论】:
-
如果您的用户群很大,请在 user_roles 中的每个 FK 上建立索引。坚持使用 3nf(正如您所做的那样)并且不在用户行上指定角色是很好的。
标签: sql postgresql database-design database-schema