📖 Scenario: You are setting up a simple role-based access control system for a small company's database. Different users will have different roles such as 'manager' and 'employee', and each role will have specific permissions on a table called projects.
🎯 Goal: Build a MySQL setup that creates roles, assigns privileges to those roles, and then grants those roles to users. This will help control who can read or modify the projects table.
📋 What You'll Learn
Create a role called manager_role
Create a role called employee_role
Grant SELECT and UPDATE privileges on the projects table to manager_role
Grant only SELECT privilege on the projects table to employee_role
Create two users: alice and bob
Grant manager_role to alice
Grant employee_role to bob
💡 Why This Matters
🌍 Real World
Role-based access control is used in companies to manage who can see or change data in databases securely.
💼 Career
Database administrators and developers use roles to simplify permission management and improve security.
Progress0 / 4 steps
1
Create roles manager_role and employee_role
Write SQL statements to create two roles named manager_role and employee_role.
MySQL
Hint
Use CREATE ROLE role_name; to create each role.
2
Grant privileges to the roles
Grant SELECT and UPDATE privileges on the projects table to manager_role, and grant only SELECT privilege on the projects table to employee_role.
MySQL
Hint
Use GRANT privileges ON table TO role_name; to assign privileges.
3
Create users alice and bob
Create two users named alice and bob with passwords 'alicepass' and 'bobpass' respectively.
MySQL
Hint
Use CREATE USER 'username'@'host' IDENTIFIED BY 'password'; to create users.
4
Grant roles to users
Grant the role manager_role to user alice and the role employee_role to user bob.
MySQL
Hint
Use GRANT role_name TO 'username'@'host'; to assign roles to users.
Practice
(1/5)
1. What is the main purpose of role-based access control (RBAC) in MySQL?
easy
A. To store user passwords securely
B. To group permissions into roles and assign them to users
C. To optimize query performance
D. To backup the database automatically
Solution
Step 1: Understand role-based access control concept
RBAC groups permissions into roles instead of assigning them individually to users.
Step 2: Identify the main purpose in MySQL context
Assigning roles to users simplifies permission management and improves security.
Final Answer:
To group permissions into roles and assign them to users -> Option B
Quick Check:
RBAC = Group permissions + assign roles [OK]
Hint: RBAC groups permissions, then assigns roles to users [OK]
Common Mistakes:
Confusing RBAC with password storage
Thinking RBAC improves query speed
Assuming RBAC is for backups
2. Which of the following is the correct syntax to create a role named manager in MySQL?
easy
A. CREATE ROLE manager;
B. MAKE ROLE manager;
C. ADD ROLE manager;
D. NEW ROLE manager;
Solution
Step 1: Recall MySQL syntax for creating roles
The correct command to create a role is CREATE ROLE role_name;.
Step 2: Match syntax with options
Only CREATE ROLE manager; uses the correct CREATE ROLE syntax.
Final Answer:
CREATE ROLE manager; -> Option A
Quick Check:
CREATE ROLE = create new role [OK]
Hint: Use CREATE ROLE to make a new role in MySQL [OK]
Common Mistakes:
Using MAKE ROLE instead of CREATE ROLE
Using ADD ROLE which is invalid
Using NEW ROLE which is not a MySQL command
3. Given the following commands:
CREATE ROLE analyst;
GRANT SELECT ON sales TO analyst;
GRANT analyst TO alice;
What permission does user alice have on the sales table?
medium
A. SELECT permission on sales
B. INSERT permission on sales
C. No permissions on sales
D. ALL permissions on sales
Solution
Step 1: Analyze role creation and permission grant
The role analyst is created and given SELECT permission on the sales table.
Step 2: Check user role assignment
User alice is granted the analyst role, so she inherits its permissions.
Final Answer:
SELECT permission on sales -> Option A
Quick Check:
User with role gets role's permissions [OK]
Hint: User inherits permissions from assigned roles [OK]
Common Mistakes:
Assuming user gets INSERT without grant
Thinking user has no permissions without direct grant
Assuming ALL permissions without explicit grant
4. Consider this MySQL command:
GRANT SELECT ON employees TO 'john';
Why might this command fail when using role-based access?
medium
A. Because 'john' is not a valid username format
B. Because SELECT is not a valid permission
C. Because GRANT cannot be used on tables
D. Because permissions should be granted to roles, not directly to users
Solution
Step 1: Understand role-based access best practice
In RBAC, permissions are granted to roles, then roles are assigned to users.
Step 2: Identify why direct grant to user may fail
Granting permissions directly to users bypasses roles and may be restricted or discouraged.
Final Answer:
Because permissions should be granted to roles, not directly to users -> Option D
Quick Check:
Grant permissions to roles, assign roles to users [OK]
Hint: Grant permissions to roles, not users directly [OK]
Common Mistakes:
Thinking SELECT is invalid permission
Assuming username format is wrong
Believing GRANT can't be used on tables
5. You want to create a role developer with INSERT and UPDATE permissions on the projects table, then assign it to user emma. Which sequence of commands correctly achieves this?
hard
A. CREATE ROLE developer; GRANT SELECT ON projects TO developer; GRANT developer TO emma;
B. GRANT INSERT, UPDATE ON projects TO emma; CREATE ROLE developer; GRANT developer TO emma;
C. CREATE ROLE developer; GRANT INSERT, UPDATE ON projects TO developer; GRANT developer TO emma;
D. CREATE USER developer; GRANT INSERT, UPDATE ON projects TO developer; GRANT developer TO emma;
Solution
Step 1: Create the role named developer
Use CREATE ROLE developer; to define the role.
Step 2: Grant INSERT and UPDATE permissions on projects to the role
Use GRANT INSERT, UPDATE ON projects TO developer; to assign permissions.
Step 3: Assign the role to user emma
Use GRANT developer TO emma; to give emma the role's permissions.
Final Answer:
CREATE ROLE developer; GRANT INSERT, UPDATE ON projects TO developer; GRANT developer TO emma; -> Option C
Quick Check:
Create role, grant permissions, assign role [OK]
Hint: Create role, grant permissions, then assign role to user [OK]
Common Mistakes:
Granting permissions directly to user instead of role
Using CREATE USER instead of CREATE ROLE
Granting wrong permissions like SELECT instead of INSERT, UPDATE
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.