Different Users role , each in one table - database

in my database i have admins , merchants and drivers .
All of them share many attributes like name/email/phone/credentials..etc
So , the way i designed my database is a tabled called "users" and table for each role [admin,merchant,driver] where shared attributes are stored in users table.
My questions is do i have to maintain an ID for each role for example [ driver_id , merchant_id ] and link it to the user_id or depending only on the user_id and storing the type of the user in users table ?

Depending on the type of queries you'll run on your database, you could have a few different options. If you're going to query the user table without needing information from the role-specific tables, then just store the type of user i.e. role in the users table.
However, if you are going to be including role-specific information that is in the admin, merchant, or driver tables, then it may be beneficial to include a unique merchant_id, admin_id or driver_id in the user table to designate a role and simultaneously act as ID into the appropriately associated table.
Diagramming this in database modeling software can help to identify the relationships between different data pieces within your application to better design your tables and attributes.

Related

DB Design: user and role tables and/or specific user type tables?

I have a specific application where users could have different roles. Apart from different permissions controlled by the business logic, certain roles of users need additional linked info. For example, the Requestors are linked to departments.
So the question is which is the better way to go aa far as the design goes:
1
User table with department_id linked to the Department table that will be null for non requestors.
Role table with user roles
UserRole linking table
2
User table
Requestor table with department_id linked to departments and user_id linked to users
Role table with user roles
UserRole table
So, in other words, for the special types of roles that might need to have or be linked to additional data, do I create separate tables, or bunch all together inside the user table and handle the rest via the roles and the application logic?
Hope all the user can't be requester. Also If you link dept and user then dept_id will be FK in user table that wont be good. Also in future if you need to link some other info then you have add another attribute in user table. Bridge table between user and department will be good same way like userRole bridge table. If the user is requester then bridge table will have record for that user (user_id and dept_id). In future if you want to create different link then you can create another bridge table and maintain instead of adding attribute in user table

multiple users Database design

I'm creating a db schema that involves users that can have multiple users.
I want to register different companies to use the web services.
For example:
user A or B (etc) can signup and create a company account
user A can create multiple accounts of other users with their types, similarly user B
If user A or B create different accounts, how would I know this particular user is belong to User A or B company ? I think user table have many to many relationship with itself (like basic friendship design).
Please suggest the best design .
Ex.
User 3,4 belongs to User A
User 5,6 belongs to User B
In general, I would recommend starting by identifying all the entities you are trying to persist. It sounds like you have two distinct entities in your question. One being "user," which represents a single person. Your second entity is "company." A "user" can belong to a company.
An example of a database design would be one table for users, and one table for companies. In the "users" table, you would want to have a foreign key column that references the primary key (unique id) of the company the user belongs to. If each user can only belong to one company, this becomes a simple one to many relationship.
In short, I would highly recommend treating company accounts separately from user accounts, since they are fundamentally different entities.

Database Tables - To decouple or not?

Is it better to create tables that store a lot of data that are related to an entity (User for example) or many tables to store said data?
For example:
User Table
Name
Email
Subscription Id
Email Notifications
Permissions
Or
User Table
Name
Email
Subscription Table
User ID
Subscription ID
Notification Table
User ID
Receives?
... etc
Please consider code in this as well, or I would have posted to ServerVault.
From a relational design standpoint what is important is the normal form you're aiming for. In general, if the "column" would require multiple values (subscription_id1, subscription_id2, etc) then it is a repeating group, and that would indicate to you that it needs to be moved to a related table. You've provided very general table and column notes, but taking a cue from the fact that you named "Email Notifications" and "Permissions" with plurals, I'm going to assume that those require related tables.

Table naming conventions for ref tables vs storage tables

By ref table I'm referring to a kind of entity 'type' table,
and by storage table I'm referring to a table that stores a lot of changing information.
For example:
I have a 'user' table named as such, which is a storage table since it can hold an indeterminate amount of users.
Then i have 'roles' table, which holds role information, it is a type table, as there are many users for each role.
I then have a 'profiles' table, which hold a one to one relationship with the 'user' table.
Now, I've tried this:
user
userrole
userprofile
However, this convention to me implies that the roles and profile tables each have a one to one relationship with user whereas I know the role table does not.
How to people usually name tables for semantic purposes for the example I described?
The Oracle convention works well here.
plural for normal tables (Users for a table of Users. The table itself is a store of many users, so I name it as I would describe the data within it)
User_Roles would be a distinct list of roles.
User_Role_Assignments a list of roles for users. I would imagine you would want a many to many here if a user can have any role and a role can be assigned to any user.
User_Profiles would be a distinct list of profiles.
User_Profile_Assignments for a table which was a many to many relationship between users and profiles.
If you have a one to one relationship, then one of the tables should have a key to the other. Users should have a profile_id if there is a one to one relationship.

How to model this one-to-one relation?

I have several entities which respresent different types of users who need to be able to log in to a particular system. Additionally, they have different types of information associated with them.
For example: a "general user", which has an e-mail address and "admin user", which has a workstation number (note that this a hypothetical case). Both entities also share common properties like first name, surname, address and telephone number. Finally, they naturally need to have a (unique) user name and a password to log in.
In the application, the user just has to fill in his user name and password, and the functionality of the application changes slightly according to the type of the user. You can imagine that the username needs to be unique for this work.
How should I model this effectively?
I can't just create two tables, because then I can't force a unique constaint on the user name.
I also can't put them all in just one table, because they have different types of specific information associated to them.
I think I might need 3 seperate tables, one for "users" (with user name and password), one for the "general users" and another one for the "admin users", but how would the relations between these work? Or is there another solution?
(By the way, the target DBMS is MySQL, so I don't think generalization is supported in the database system itself).
Your 3 tables approach seems Ok.
In users table have only ID, username, password,usertype.
In general users table have ID, UserID (from users table), other fields.
Same thing for admin users.
Usertype field will tell you from what table to search for additional info
if(usertype==admin)
select * from admins where userid=:id;
else
select * from general where userid=:id;
Two tables. USERS with user names, first, last, etc. ROLES with roles, and a link back to the user name (or user id or whatever). Put a unique constraint on the user name. Put workstation nbr, email, phone, whatever else you need, in the user table. Put 2 columns in the ROLES table -- USERID and ROLE.
You should decide how much specific information is being stored (or likely to be stored in the future) and make the decision based on that. If there are only a handful of fields for each user type then using a single table is alright.
USERS table (name, type, email, password, genfield1, genfield2, adminfield1, adminfield2)
Make sure to include the type (don't assume because some of the fields particular to that user are filled in that the user is of that type) field. Any queries will just need to include the "AND usertype = " clause.
If there are many fields or rules associated with each type then your idea of three tables is the best.
USERS table (ID, type, name, password)
GENUSERS (ID, genfield1, genfield2)
ADMINUSERS(ID, adminfield1, adminfield2)
The constraints between IDs on the table are all you need (and the main USERS table keeps the IDs unique). Works very well in most situations but reports that include both types of users with their specific fields have to be done in two parts (unioned SQL or subqueries or multiple left joins).
You can solve it with one 'general' users table containing the information thats available for all users and 1 table for every specific user type. In your example you will then need 3 tables.
Users: This table holds only information shared between all usertypes, ie. UserId, Name, Address, etc.
GeneralUsers: This table 'extends' the Users table by providing a foreing key UserId that references the Users table. In addition, information specific to general users are held here, fx. EmailAddress, etc.
AdminUsers: As with GeneralUsers, this table also 'extends' the Users table by providing a foreign key UserId referencing the Users table. In addition information specific to admin users are held here, fx. WorkstationId, etc.
With this approach you can add additional 'specializations' if the need arises by simply adding new tables that 'extends' the Users table using a foreign key reference. You can also create several levels of specialization. If for example admin users are general users as well as admin users then AdminUsers could 'extend' GeneralUsers instead of Users simply by using a foreing key to GeneralUsers instead of Users.
When you need to retreive data from this model you need to which type of user to query. If for example you need to query a GeneralUser you will need something similar to:
SELECT * FROM GeneralUsers
LEFT JOIN Users ON GeneralUsers.UserId = Users.UserId
Or if querying an admin user
SELECT * FROM AdminUsers
LEFT JOIN Users ON AdminUsers.UserId = Users.UserId
If you have additional levels of specialization, for example by having admin users also being general users you just join your way back.
SELECT * FROM AdminUsers
LEFT JOIN GeneralUsers ON AdminUsers.UserId = GeneralUsers.UserId
LEFT JOIN Users ON GeneralUsers.UsersId = Users.UserId
I most definitely would not do a model where you have separate tables as in GeneralUser, AdminUser and ReadOnlyUser.
In database design, a good rule of thumb is "Down beats across". Instead of multiple tables (one for each type), I would create a SystemUsers table, and a Roles table and define a join table to put SystemUsers in Roles. Also, I would define individual roles.
This way, a user can be added to and removed from multiple roles.
A role can have multiple permissions, which can be modified at any time.
Joins to other places do not need a GeneralUserId, AdminUserId and ReadOnlyUserId column - just a SystemUserId column.
This is very similar to the ASP.Net role based security model.
alt text http://img52.imageshack.us/img52/2861/rolebasedsecurity.jpg

Resources