A
A
Artem00712017-08-02 14:10:45
MySQL
Artem0071, 2017-08-02 14:10:45

How to split a table correctly?

There is a users table, I want to add rules (access to individual categories)
I.e. I have 2 options
1st option: add directly to the table
And then there will be users(user_id, user_hash, role_admin, role_moderator, role_submitter ...)
each role_* is a tinyint(1) type field where 1 or 0, because a person can be an admin and moderator, etc.
2.1 option:
add a rules table separately and make a primary key user_id
2.2 option
-//- and make a primary key rule_id and make the foreign key user_id and make it unique?
Which option would be better (and why (not necessarily, just for self-development))?
Sub-question:
If I want to make UUID keys, is it correct to do this:
user_id - int autocrement
user_hash - UUID key
And then, if accessed from outside, then by user_hash, and if inside the database, then by id
Is it correct to do this, or is it better to leave something alone?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2017-08-02
@tsklab

Which option would be better
The third.
make foreign key user_id and make it unique?
and at the same time
maybe a person can be both an admin and a moderator, etc.
will not work.
Need a table: (id, user_id, rule_id ), the last two fields in the pair are unique.

S
Stalker_RED, 2017-08-05
@Stalker_RED

table user_has_role
user_id | role_id
------------------
1       |  1
2       |  1
2       |  7


select role_id from user_has_role where user_id = 2

Or even RBAC , but it's a bit more complicated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question