[ACCEPTED]-How to connect the users table to the roles table (using the table user_roles)?-database

Accepted answer
Score: 12

You would first populate the roles table. Then, add 6 a user to the users table. Then, taking the ID 5 from the users table, you want to associate it 4 with an ID from the roles table inside of the 3 user_roles table. Like so:

---- Users Table ---------
ID | UserName | Password
 1 | Test     | *****
--------------------------

---- Roles Table ---------
ID | Role
 1 | Test_Role
 2 | Another_Role
--------------------------

---- User Roles Table ---------
UserID | RoleID
     1 |      1
     1 |      2
-------------------------------

This is done for a "Many 2 To Many" relationship. It's also called 1 "Normalizing" your database.

Score: 1

For each user, you simply insert one row 4 in the user_roles table for every role you 3 want to assign to that given user. There's 2 nothing automatic about it. It's a many-to-many 1 relationship.

More Related questions