views:

48

answers:

3

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

+2  A: 

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)
David Stratton
I think I get you. One Account table for basic information shared by them all, then 3 different tables for each of them with specific information, then I link to them with a foreign_key in the basic table right?
never_had_a_name
Yeah, you already knew it,
David Stratton
I was thinking you meant one table for administrators and another for custmers with all repeating info. I read it wrong, didn't I?
David Stratton
+1  A: 

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.

Damir Sudarevic
A: 

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
Nix