Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User, customer, admin account in 3 different tables?

In my web application I will have three types of accounts.

  • User: for using the web application for free
  • Customer: for advertising and getting a Company Logo
  • Admin: for editing and deleting stuff

Should all these three be in separate tables or in one with a column named "account_type" where i can mark it as User, Customer or Admin?

What are the pros and cons for both? Is there a best practice for this?

Thanks

like image 397
never_had_a_name Avatar asked Aug 18 '10 11:08

never_had_a_name


3 Answers

In general, a person can be user, customer and admin -- so, I would start with a Person table with columns IsCustomer, IsUser, IsAdmin. Later (for fast search) you may decide to add separate tables Admin, Customers, Users with FK to the Person table.

EDIT:

A typical case may be:

  • 5 million users
  • 1000 customers
  • 10 admins

In general, having separate tables for customers and admins should speed-up any admin/customer related query.

like image 133
Damir Sudarevic Avatar answered Oct 03 '22 17:10

Damir Sudarevic


If a user can only be one type, you'd be better off with one table and a bit field for IsAdministrator, etc.

If a user can be of more than one account type, you should then have a different table with a foreign key,

sample structure (data sypes are SQL Server and suggested only)

Users table

  • UserID - int
  • Username - varchar(25)
  • Password - varchar(25)
  • Firstname - varchar(50) etc...

Roles table

  • RoleId - int
  • Role Description - varchar(25)

User_Roles table

  • UserId - int (with a foregin key to the Users table)
  • RoleId int (foreign key to the Roles table)
like image 38
David Avatar answered Oct 03 '22 15:10

David


Pros and Cons vary based on the size and complexity of your system.

I would break it up into User, Role, UserResources

User (would define basic information)

User Roles

  • FK->RoleType

Role_Type (user, admin, customer, possibly permissions or you could break this out further).

UserResources (media)

  • FK->User
like image 32
Nix Avatar answered Oct 03 '22 16:10

Nix