简体   繁体   English

数据库设计:数据库中的用户表必须包含哪些字段?

[英]database design: what fields are must for a user table in database?

I am trying to design a user table for MySQL. 我正在尝试为MySQL设计一个用户表。

for now, my user table looks like this 现在,我的用户表看起来像这样

users (
BIGINT id,
VARCHAR(?) username,
VARCHAR(?) password,
VARCHAR(254) email,
DATETIME last_login,
DATETIME data_created
)

what other fields should I also include and why do I need them? 我还应该包括哪些其他领域?为什么我需要它们?

what fields should I exclude from above and why? 我应该从上面排除哪些字段?为什么?

how many characters should I allocate for username and password, and why? 我应该为用户名和密码分配多少个字符,为什么?

should I use BIGINT for id? 我应该使用BIGINT作为身份证吗?

Thank you in advance for your helps. 提前感谢您的帮助。

ADDED I am going to use the table for social web site, so 'users' mean people around the world. 添加我将使用该表用于社交网站,因此“用户”指的是世界各地的人们。

A few comments: 一些评论:

  1. BIGINT is fine. BIGINT很好。 I assume you're using it as a surrogate key. 我假设你正在使用它作为代理键。 In that case, declare it as 在这种情况下,将其声明为

    BIGINT id primary key auto_increment, BIGINT id主键auto_increment,

    Mysql will automatically allocate a unique int value to your id whenever you do an insert (don't specify any value for this field). 每当执行插入操作时,Mysql都会自动为您的id分配一个唯一的int值(不要为此字段指定任何值)。 Never try to implement this behaviour by selecting the max id and adding 1 to it (I've seen this so many times). 永远不要尝试通过选择max id并向其添加1来实现此行为(我已经看过很多次了)。

  2. Length of username and password: this is no big deal really, just pick a length. 用户名和密码的长度:这真的没什么大不了的,只需选择一个长度。 I tend to standardise on 255 length varchars for these things but that's not based on anything empirical. 我倾向于为这些东西标准化255个长度变量,但这不是基于经验的任何东西。

  3. Call your table "user", not "users". 将您的表格称为“用户”,而不是“用户”。 Conceptually, a table implements an entity in the same way that a class implements an entity. 从概念上讲,表以与实现实体相同的方式实现实体。 You will likely create a class or data structure called "user" and the table name should correspond to this. 您可能会创建一个名为“user”的类或数据结构,表名应与此对应。

  4. Every table that I create has two timestamps, "created" and "last_updated". 我创建的每个表都有两个时间戳,“created”和“last_updated”。 You're halfway there:) 你在那里:)

  5. I don't think I would store last_login in the user table, this is likely to be something that you will want to log in a separate table. 我不认为我会将last_login存储在用户表中,这可能是您要在单独的表中登录的内容。 It's a good idea to store all login events (login, logout, failed attempt, account lock etc.) in a logging table. 在登录表中存储所有登录事件(登录,注销,失败尝试,帐户锁定等)是个好主意。 This will give you much better visibility of what the system has been doing. 这将使您更好地了解系统一直在做什么。

1/ Username and password: decide for yourself how large you want these to be. 1 /用户名和密码:自己决定你想要的大小。

2/ BIGINT is fine, even though an integer probably suffices. 2 / BIGINT很好,即使整数可能就足够了。 But make it UNSIGNED and probably AUTO_INCREMENT, too. 但是也可以将它设为UNSIGNED,也可以是AUTO_INCREMENT。

3/Try keeping your Users table as small as possible: 3 /尽量保持您的Users表尽可能小:

users (
BIGINT id,
VARCHAR(?) username,
VARCHAR(?) password,
VARCHAR(254) email,
DATETIME data_created
)

The rest, you put in extra tables: 其余的,你加入额外的表格:

logins (
BIGINT loginid
BIGINT userid
DATETIME last_login,
VARCHAR(15) IP_ADRESS
...
)

This way, your users table will only change when a new user is added or deleted, or when someone changes his password, which is less frequently then when someone logs in . 这样, 只有在添加或删除新用户时,或者有人更改密码时,您的用户表才会更改 ,这在某人登录时较少 This allows for better table caching (MySQL clears the table cache when you write to the table). 这允许更好的表缓存(MySQL在写入表时清除表缓存)。

All that just depends on your own specs. 所有这些都取决于您自己的规格。 For username you could take 100 if you like, for password take the length of the hashing function you want to use (32 for MD5). 对于用户名,如果您愿意,可以取100,因为密码取您想要使用的散列函数的长度(对于MD5为32)。

It's more common to use INTEGER(10) with AUTO_INCREMENT on the primary key of a table. 在表的主键上使用带有AUTO_INCREMENT的INTEGER(10)更为常见。

You might want to ask for a name, surname, birth date, place of living, etc. Think that all the data you ask the user for should be somehow important to the platform that you are building. 您可能想要询问姓名,姓氏,出生日期,居住地等。请认为您要求用户提供的所有数据对您正在构建的平台而言应该是某种重要的。

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

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