一个用户可以有3种类型的配置文件: A、B、C
对于每种类型的配置文件,用户可以具有不同的名称、照片集和描述。
这应该如何建模?
在单个表中分隔列:
profile
---
name_A name_B name_C photos_A photos_B photos_C description_A description_B description_C
加入另一个表:
profile
---
A_profile_id B_profile_id C_profile_id
A_profile
---
id name photos description
B_profile
---
id name photos description
C_profile
---
id name photos description
jsonb按字段:
profile
---
name{A,B,C} photos{A,B,C} description{A,B,C}
jsonb按配置文件类型:
profile
---
A_profile{name,photos,description} B_profile{name,photos,description} C_profile{name,photos,description}
嵌套的jsonb (试探性...):
profile
---
profile{A{name,photos,description},B{name,photos,description},C{name,photos,description}}
发布于 2018-06-08 04:21:21
如果你打算有一组固定的不同的配置文件,那么利用关系数据库的优势,你应该选择"Join Another Table“选项。
其他每个选项都稍微更灵活一些,但本质上只是一个非规范化的版本。
发布于 2018-06-08 04:57:17
假设所有的配置文件都是相同的,那么我首先想到的是两个表:
create table Profiles (
profileId serial,
. . .
);
create table Persons (
personId serial,
. . .
profile_A int references profiles(profileId),
profile_B int references profiles(profileId),
profile_C int references profiles(profileId)
);
尽管有三个不同的列可能有很好的理由,但您可能还需要一个连接表:
create table personProfiles (
personProfileId serial,
personId int references person(personId),
profileId int references profiles(profileId),
profileTye <whatever>
);
https://stackoverflow.com/questions/50749142
复制相似问题