开发者

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

I am trying to design a user table for 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 sho开发者_如何学JAVAuld I exclude from above and why?

how many characters should I allocate for username and password, and why?

should I use BIGINT for id?

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. I assume you're using it as a surrogate key. In that case, declare it as

    BIGINT id primary key 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). Never try to implement this behaviour by selecting the max id and adding 1 to it (I've seen this so many times).

  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.

  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.

  4. Every table that I create has two timestamps, "created" and "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. 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.

2/ BIGINT is fine, even though an integer probably suffices. But make it UNSIGNED and probably AUTO_INCREMENT, too.

3/Try keeping your Users table as small as possible:

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).


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).

It's more common to use INTEGER(10) with AUTO_INCREMENT on the primary key of a table.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜