在 SQL 表中为每个用户设置一列,为每个用户设置一行是一种好习惯吗?

עומ*_*סון 0 sql database

我目前正在设计一个“tinder”克隆,我有一个用户数据库,我想存储某个用户是“向右滑动”(喜欢)另一个用户还是“向左滑动”(不喜欢)或根本没有看到.

我想制作表格,每一行都是不同的用户,每一列都是不同的用户,所以如果我将值 1 存储在 Bob 的行和 Alice 的列中,我知道 Bob 在 Alice 上向右滑动。所以我的问题是,这是好的做法吗?对于 n 个用户,我必须存储 n^2 个值,这似乎是错误的(但我想这是不可避免的)。

最重要的是,有很多冗余信息,因为有些人对某个性别没有性吸引力,因此某些价值观永远不会被访问。

这是我第一次使用 SQL,所以我现在有点迷茫。这样做的更好方法是什么?

Gor*_*off 5

不,这是个坏主意。

  • 添加新用户需要更改表的结构8
  • 您只能存储一项信息,但每次滑动可能会存储更多信息。
  • “第二个”用户的身份并不清楚,因为它嵌入在列名中。

正确的做法是让表格每次滑动一行:

create table swipes (
    swipeId int primary key,   -- auto-incremented/identity/serial
    FromUserId int not null references users(user_id),
    ToUserId int not null references users(user_id),
    -- and then additional information you might want
    swipeTime timestamp,
    direction varchar(10)
);
Run Code Online (Sandbox Code Playgroud)