Search code examples
mysqlsqldatabasefieldtype

Best way to store a one-to-many column in database


I have a users table in my database and have three kinds of user roles - manager(admin), expert, and student.

I have tried storing their role as column role in the users table, but my question is what is the best way to store it?

For example, should I use numbers - 1 for admin, 2 for expert and 3 for student. Or only one character - 'A' for admin , 'E' for expert and 'S' for student? Or full string 'admin' for admin and so on, or what?

Edit

Guys, I want most efficient way. Some of you told me to have another table but this relationship isn't many to many, it's one to many because users can only have one role.


Solution

  • You might want to have a separate junction table, eg user_roles that will have columns: id, user_id and role_id. This way each user can have multiple roles in the future, even if right now they only have 1.

    And that role_id would point to a role in a new roles table, which could have all the data associated with the roles.

    Or if you really don't want the junction table, then just put the role_id directly into your users table.

    Look up database "normalization". Basically if you have a specific set of data that will be repeating in a column, you might want to put it in its own table.