Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to have two separate user tables or one?

My web app is going to have two types of users that are 100% different - in fields, functions, and purpose on the site. The purpose of one type of user is to post blogs, and the purpose of the other is to read them and "follow" the posters.

The ONLY stuff they have in common are the need for an id, email, password and a couple other bits of meta data such as date joined, etc.

Should I attempt to stem them from the same account table, or make one table for each?

NOTES

-One potential thing to think about is that, to prevent user confusion, I think emails between the two types of accounts should be unique.

-Readers and Authors will have different sets of data that will need to be stored about them aside from the normal "account" meta data.

like image 228
johnnietheblack Avatar asked Dec 27 '22 13:12

johnnietheblack


2 Answers

I would separate them.

TABLE User {
    id
    email
    password
    salt
    created
    modified
}

TABLE follower {
    user_id
    ...
}

TABLE author {
    user_id
    ...
}

Over time your application will grow. Perhaps now you have two destinct roles - but it may change in the future. You may have authors that also follow other authors, or followers that are "upgraded" to authors. You might also start adding other "roles" and want something like this:

TABLE user_role {
    user_id
    role_id
}

TABLE role {
    id
    name
}
like image 112
Xeoncross Avatar answered Dec 30 '22 03:12

Xeoncross


Define "user". If a user is somebody who has registered him or herself on your site, using an e-mail address, password and nickname, and who can log on, then stick everyone in the users table.

The things users can or can not do on a site does not differ for a user. It's their permissions that are different. Map permissions to users in a separate table. Don't create a table for each type of user.

Otherwise, when you're adding a new kind of permission in the future, you don't have to add a new table for that type of user (and alter all (sql) code that handles with users: logging in, resetting passwords, and so on). You just add a new type of permission, and map that to their respective users.

like image 30
CodeCaster Avatar answered Dec 30 '22 03:12

CodeCaster