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

Joon picture Joon · Apr 11, 2011 · Viewed 19.9k times · Source

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 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?

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.

Answer

PhilDin picture PhilDin · Apr 11, 2011

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.