0

I have table like below.

enter image description here

I want to store user roles like admin,reader,writer

How can I store the values in my table.

I searched in google and many results such as 2 types.

  1. Storing values to a single column name as user_roles and store values with pipe (|) separated (same as above).
  2. Storing values on another table like user_roles and store with foreign key of users.

Which of the above two method is better for development ?

Tell me the advantage and dis-advantage of both please...

Thanks & Regards

8
  • 2
    The second option - the 1st makes it much harder to query on the individual fields, for example if you wanted to show a list of all users with the role writer Commented Dec 15, 2015 at 17:07
  • A quick google for "database normalization" would be beneficial Commented Dec 15, 2015 at 17:09
  • It really depends on how big a system you are going to make, the smartest way would be to have a sperate table for permissions, but if your system is just a small one and not going to be expanded that much, then just save it in the same table. Commented Dec 15, 2015 at 17:10
  • @uruloke small projects get bigger, the opposite is rarely true Commented Dec 15, 2015 at 17:10
  • @Steve, But the size of database is become large due to this right ? Commented Dec 15, 2015 at 17:11

2 Answers 2

1

The reasons you want the second choice include, mainly

  • Data Normalization Sanity
  • Speedy use of indexes and not table scans
  • Avoiding coding nightmares like find_in_set()

See Junction Tables or associations tables (more simplified)

See Nightmare Coding without it (and slow performance as all get up).

Sign up to request clarification or add additional context in comments.

Comments

0

If you user is having more than one user roles, so definitely it will define as one to many relationship.

So adding table with permutations alone with user Id is best option. Cz one user can have many user roles.


this kind of combination never happens (one-to-one)

user A - admin
User B - writer
User C - reader

01

Possible is (one-to-many) or (many-to-one)

User A- Admin + reader
User B - reader + writer
user C - Admin + reader + writer
user D - Admin + writer
.....

02

as well as read these too

  1. One to Many and Many to One Relationships - code.tutsplus.com
  2. One-to-many relationship - www.databaseprimer.com/
  3. Database Normalization

4 Comments

I want the reason for choosing the one to many relation other than first one. can you please tell me that..:)
Abdulla, you are not giving sound technical reasons. You can save data for one-to-many in a text file if you want. He is not asking how you can save data. He is asking what are the best practices and why. You are merely saying: "Data can be saved"
@Drew sorry i didnt get you
@JishadP did my answer helpfull?? if helpful accept + vote me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.