We see there is 3 tables

  1. users
  2. role
  3. users_roles

May I ask where there is 3 table, but why not 2 tables, and put uid in role table, and rid in users table ?

Can some one make it clear for us. Thanks.

May i know what reason behind this design structure?
May I know how many records for a normally-designed database table can be called a 'big data', or how many bytes of data can be called 'big data' which result a 'slow query'?

Thanks.

Comments

Jaypan’s picture

Users defines user. Ex:
* Jaypan (uid 1)
* qqboy (uid 2)

Roles defines roles. Ex:
* admin (rid 3)
* moderator (rid 4)

users_roles connects the two. Ex:
UID | RID
1 | 3 (Jaypan is an admin)
1 | 4 (Jaypan is a moderator)
2 | 4 (qqboy is a moderator)

This structure allows for multiple users to have multiple roles. It's called a many-to-many relationships.

May I ask where there is 3 table, but why not 2 tables, and put uid in role table

If the uid was in the role table, only one UID could be given for each role. This means only one person could be assigned to each role.

and rid in users table ?

If you put rid in users table, it means each user could only be assigned one role. This means that users could not have multiple roles.

qqboy’s picture

If the uid was in the role table, only one UID could be given for each role. This means only one person could be assigned to each role.

Not exactly, for example, you can put rid of 1 and 2 to one user by rid 1-2-58-12 what ever what as you like.
What do you think ?

Jaypan’s picture

You mean multiple values in a single column?

qqboy’s picture

Jaypan’s picture

All sorts of problems. For example, if I want to remove the admin role from Jaypan, I would use:

DELETE FROM {users_roles} WHERE uid = 1 AND rid = 3

How would you do that if multiple values were packed in the same column? I'm not even sure it could be done in a single query.

And what about selecting all users of a role? This wouldn't work:

SELECT uid FROM {users} WHERE role LIKE "%3%"

because you'd get users with role 3, 33, 333 etc.
And this wouldn't work:
SELECT uid FROM {users} WHERE role LIKE "%-3-%" because if your column looked like 3-4-5, it would not find it.

Also, you couldn't index the column on the role. So even if you found a query that allowed you to select all users of a given role, you couldn't index the column properly to ensure that query worked quickly.

There are many other issues too. It's a bad practice to store multiple values in a single column.

Hopefully that clears it up for you!

qqboy’s picture

ok
what you said is reasonable.
Thanks.
You are so gooooooooooooood.

Jaypan’s picture

Thanks :)

RoloDMonkey’s picture

You may want to familiarize yourself with database normalization: https://en.wikipedia.org/wiki/Database_normalization

--

Read more at iRolo.net

qqboy’s picture

called database normalization seems not very applicable in drupal.
i think there always a contradiction between redundancy and less query.

if no database reduncy, there will be many table query.
how would you balance it ?

thanks and regards.