I have seen a few database designs where it has all user information in an account table, including password, email, DOB, First Name, Last Name etc.
我见过一些数据库设计,其中包含帐户表中的所有用户信息,包括密码,电子邮件,DOB,名字,姓氏等。
I have seen some others that have two tables
我见过其他一些有两张桌子的人
username(or email), password,state(activated etc), group(admin, owner, user etc)
用户名(或电子邮件),密码,状态(激活等),组(管理员,所有者,用户等)
and
和
nameFirst, nameLast, birthDay,birthMonth,birthYear etc
nameFirst,nameLast,birthDay,birthMonth,birthYear等
What are the pro's and cons of the above methods?
上述方法的优点和缺点是什么?
4 个解决方案
#1
2
The difference between the two designs is mostly one of flexibility. If the account and user data share a single table, then each user must have an account, and each account can have only one user (unless you add another table to allow child users to be added in addition to the user that lives with the account data, or unless you add new records with duplicate account details in each one, which is very bad and antithetical to what databases are supposed to do).
两种设计之间的差异主要是灵活性。如果帐户和用户数据共享一个表,则每个用户必须拥有一个帐户,并且每个帐户只能有一个用户(除非您添加另一个表以允许除了与该帐户一起使用的用户之外添加子用户数据,或者除非你在每个数据中添加具有重复帐户详细信息的新记录,这对于数据库应该做的事情是非常糟糕和对立的)。
With two tables, you can easily have multiple users in each account, and might also choose to allow circumstances where an account has no users, or where a user does not have an account, if doing so would benefit your use-case.
使用两个表,您可以轻松地在每个帐户中拥有多个用户,并且还可以选择允许帐户没有用户的情况,或者用户没有帐户的情况,如果这样做会使您的用例受益。
The tradeoff is that if you want to do something like determine the account for a user (or the user(s) in an account), you have to do a join if you are using two tables. If you have one table all you have to do is fetch the row to get this information.
权衡是,如果您想要为用户(或帐户中的用户)确定帐户,则必须在使用两个表时进行连接。如果你有一个表,你只需要获取行来获取这些信息。
#2
1
Well, the obvious main problem is that you have to deal with two tables if you want information from both. That's likely to complicate your queries a little and possibly reduce performance.
好吧,显而易见的主要问题是如果你想要两者的信息,你必须处理两个表。这可能会使您的查询稍微复杂化并可能降低性能。
Since all that information is dependent on a single key field (username most likely here), I tend to put it al in one table except in one very specific scenario: if, for example, you wanted to give someone access to the details in the first table but not the second, you could split it up for the purposes of security (opening up the first to everyone but restricting the second to only those who need the extra detail - but I'd probably move the password to the second table in that case).
由于所有这些信息都依赖于单个关键字段(这里最常用的用户名),我倾向于将其放在一个表中,除非在一个非常具体的情况下:例如,如果您想让某人访问第一个表但不是第二个表,你可以为了安全起见将它拆开(向第一个开放第一个但是将第二个仅限于那些需要额外细节的人 - 但我可能会将密码移到第二个表中那个案子)。
Other than that, I'd minimise the number of objects as long as it didn't get in the way of maintaining third normal form.
除此之外,只要它不妨碍维持第三范式,我就会尽量减少对象的数量。
#3
0
I believe it is up to you the database designer. As long as it is not difficult for you to work with in the future via server side languages - it is a viable solution.
我相信这取决于你的数据库设计师。只要您将来通过服务器端语言使用并不困难 - 这是一个可行的解决方案。
I have set up dbs with credentials and sensitive data (encrypted) in one table and all the other stuff in another table. I have also set up dbs that have a table to house all of that data.
我在一个表中设置了带有凭据和敏感数据(加密)的dbs,在另一个表中设置了所有其他内容。我还设置了dbs,它有一个表来容纳所有数据。
Either way it takes only 1 query statement to get and/or manipulate the data from one or two tables.
无论哪种方式,它只需要1个查询语句来获取和/或操纵来自一个或两个表的数据。
#4
0
Keeping information in seperate tables if we are talking about the same 'object' (the user and her extra information belong together), is something i prefer to avoid. But I can think of two good reasons to split them up:
如果我们谈论相同的“对象”(用户和她的额外信息属于一起),将信息保存在单独的表格中,我更愿意避免。但我可以想出两个很好的理由将它们分开:
-
If you have designed, or are using, a seperate authentication system with its own table, e.g.
User
, but you need to add additional information. Or you have a standard system, but the information/fields for a user depends on your client: names of fields, amount of fields ... Then you can keep the authentication part standard, and the extra information part is known to be flexible.如果您已经设计或正在使用具有自己的表的单独身份验证系统,例如用户,但您需要添加其他信息。或者您有一个标准系统,但用户的信息/字段取决于您的客户端:字段名称,字段数量......然后您可以将身份验证部分保持标准,并且已知额外信息部分是灵活的。
-
If inside your data-model you have an elaborate
Person/People
model, with adresses, birthdates, what have you not, you could choose to use the same table to store that information for your users as well. So youuser
would then also have aperson_id
or something similar.如果你的数据模型中有一个精心制作的人/人模型,有地址,生日,你没有,你可以选择使用同一个表来为你的用户存储这些信息。因此,您的用户也将拥有person_id或类似的东西。
Hope this helps.
希望这可以帮助。
#1
2
The difference between the two designs is mostly one of flexibility. If the account and user data share a single table, then each user must have an account, and each account can have only one user (unless you add another table to allow child users to be added in addition to the user that lives with the account data, or unless you add new records with duplicate account details in each one, which is very bad and antithetical to what databases are supposed to do).
两种设计之间的差异主要是灵活性。如果帐户和用户数据共享一个表,则每个用户必须拥有一个帐户,并且每个帐户只能有一个用户(除非您添加另一个表以允许除了与该帐户一起使用的用户之外添加子用户数据,或者除非你在每个数据中添加具有重复帐户详细信息的新记录,这对于数据库应该做的事情是非常糟糕和对立的)。
With two tables, you can easily have multiple users in each account, and might also choose to allow circumstances where an account has no users, or where a user does not have an account, if doing so would benefit your use-case.
使用两个表,您可以轻松地在每个帐户中拥有多个用户,并且还可以选择允许帐户没有用户的情况,或者用户没有帐户的情况,如果这样做会使您的用例受益。
The tradeoff is that if you want to do something like determine the account for a user (or the user(s) in an account), you have to do a join if you are using two tables. If you have one table all you have to do is fetch the row to get this information.
权衡是,如果您想要为用户(或帐户中的用户)确定帐户,则必须在使用两个表时进行连接。如果你有一个表,你只需要获取行来获取这些信息。
#2
1
Well, the obvious main problem is that you have to deal with two tables if you want information from both. That's likely to complicate your queries a little and possibly reduce performance.
好吧,显而易见的主要问题是如果你想要两者的信息,你必须处理两个表。这可能会使您的查询稍微复杂化并可能降低性能。
Since all that information is dependent on a single key field (username most likely here), I tend to put it al in one table except in one very specific scenario: if, for example, you wanted to give someone access to the details in the first table but not the second, you could split it up for the purposes of security (opening up the first to everyone but restricting the second to only those who need the extra detail - but I'd probably move the password to the second table in that case).
由于所有这些信息都依赖于单个关键字段(这里最常用的用户名),我倾向于将其放在一个表中,除非在一个非常具体的情况下:例如,如果您想让某人访问第一个表但不是第二个表,你可以为了安全起见将它拆开(向第一个开放第一个但是将第二个仅限于那些需要额外细节的人 - 但我可能会将密码移到第二个表中那个案子)。
Other than that, I'd minimise the number of objects as long as it didn't get in the way of maintaining third normal form.
除此之外,只要它不妨碍维持第三范式,我就会尽量减少对象的数量。
#3
0
I believe it is up to you the database designer. As long as it is not difficult for you to work with in the future via server side languages - it is a viable solution.
我相信这取决于你的数据库设计师。只要您将来通过服务器端语言使用并不困难 - 这是一个可行的解决方案。
I have set up dbs with credentials and sensitive data (encrypted) in one table and all the other stuff in another table. I have also set up dbs that have a table to house all of that data.
我在一个表中设置了带有凭据和敏感数据(加密)的dbs,在另一个表中设置了所有其他内容。我还设置了dbs,它有一个表来容纳所有数据。
Either way it takes only 1 query statement to get and/or manipulate the data from one or two tables.
无论哪种方式,它只需要1个查询语句来获取和/或操纵来自一个或两个表的数据。
#4
0
Keeping information in seperate tables if we are talking about the same 'object' (the user and her extra information belong together), is something i prefer to avoid. But I can think of two good reasons to split them up:
如果我们谈论相同的“对象”(用户和她的额外信息属于一起),将信息保存在单独的表格中,我更愿意避免。但我可以想出两个很好的理由将它们分开:
-
If you have designed, or are using, a seperate authentication system with its own table, e.g.
User
, but you need to add additional information. Or you have a standard system, but the information/fields for a user depends on your client: names of fields, amount of fields ... Then you can keep the authentication part standard, and the extra information part is known to be flexible.如果您已经设计或正在使用具有自己的表的单独身份验证系统,例如用户,但您需要添加其他信息。或者您有一个标准系统,但用户的信息/字段取决于您的客户端:字段名称,字段数量......然后您可以将身份验证部分保持标准,并且已知额外信息部分是灵活的。
-
If inside your data-model you have an elaborate
Person/People
model, with adresses, birthdates, what have you not, you could choose to use the same table to store that information for your users as well. So youuser
would then also have aperson_id
or something similar.如果你的数据模型中有一个精心制作的人/人模型,有地址,生日,你没有,你可以选择使用同一个表来为你的用户存储这些信息。因此,您的用户也将拥有person_id或类似的东西。
Hope this helps.
希望这可以帮助。