简体   繁体   English

何时使用关系数据库结构?

[英]When to use a relational database structure?

I'm creating a file hosting service, but right now I am creating the account email activation part of registering. 我正在创建文件托管服务,但是现在我正在创建注册的帐户电子邮件激活部分。 So I had to come up with a database structure. 因此,我不得不提出一个数据库结构。

And right now it's: 现在是:

users
  id
  first_name
  last_name
  email
  password
  since
  active
  hash_activate

But I can do it like a relational database too: 但是我也可以像关系数据库一样来做:

users
  id
  first_name
  last_name
  email
  password
  since

activation
  id
  user_id
  hash
  active

What would be the best way to go about it? 最好的方法是什么? And why? 又为什么呢?

The second would only be sensible if one user could have multiple activations. 仅当一个用户可以进行多次激活时,第二个才有意义。 You don't say whether this is true or false, so I couldn't possibly advise you. 您不会说这是对还是错,所以我无法建议您。

If every person has only one activation hash active at at time, then it's feasible to store it in same table with users. 如果每个人一次只有一个激活哈希,则可以将其与用户存储在同一表中。

However, one advantage of separating it is that users only have an activation hash for a brief period of time, so to keep the user records smaller, you could store the hashes in a separate table. 但是,将其分开的一个优点是用户仅在短时间内拥有一个激活哈希,因此,为了使用户记录更小,可以将哈希存储在单独的表中。 Keeping the user records small keeps it more performant. 保持用户记录较小,使其性能更高。 In this case, you wouldn't have active column. 在这种情况下,您将没有active列。 You'd just delete inactive hashes. 您只需删除不活动的哈希即可。

If you do store the activation columns in the user table, just be sure to select the columns by name. 如果确实将激活列存储在用户表中,只需确保按名称选择列即可。 Eg in most cases, you'll want do this: 例如,在大多数情况下,您需要这样做:

SELECT id, first_name, last_name, email, password
FROM users

Instead of: 代替:

SELECT *
FROM users

You'd only want to select the activation columns when you needed them. 您只需要在需要时选择激活列。

If activations are a temporary thing, or having a hash defines someone as active, then make them different. 如果激活是暂时的,或者散列将某人定义为激活,则使其与众不同。 Otherwise, that really won't matter. 否则,那真的没关系。

However, neither is necessarily more or less relational than the other, without much more information. 但是,在没有更多信息的情况下,两者之间的关系不一定或多或少。 If you put a unique constraint on the combination of values in each row, and set each column up with a NOT NULL constraint, your first one would be quite relational. 如果对每行中的值组合施加唯一约束,并使用NOT NULL约束设置每一列,则第一个约束将是非常相关的。

You use a relational design when correctness of data, over time, is as important, if not more important, than what the application does with that data, and/or when data structure correctness/consistency is critical to the correct operation of an application, but might not necessarily be guaranteed by the application's own operation. 当数据的正确性随着时间的推移与应用程序对数据的处理同样重要(如果不是更重要)时,和/或当数据结构的正确性/一致性对于应用程序的正确操作至关重要时,您可以使用关系设计。但不一定由应用程序自己的操作来保证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM