Revoking privileges in MySQL - Time & Space Complexity
When we revoke privileges in a database, the system checks and updates user permissions.
We want to understand how the time it takes changes as the number of users or privileges grows.
Analyze the time complexity of the following code snippet.
REVOKE SELECT, INSERT ON database_name.* FROM 'user'@'host';
This command removes specific privileges from a user on all tables in a database.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database system checks the privilege tables to find and update the user's privileges.
- How many times: It scans the privilege entries related to the user and the specified privileges once.
The time to revoke privileges grows with the number of privilege entries the system must check.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 privilege entries | 10 checks |
| 100 privilege entries | 100 checks |
| 1000 privilege entries | 1000 checks |
Pattern observation: The work increases directly with the number of privilege entries to check.
Time Complexity: O(n)
This means the time to revoke privileges grows linearly with the number of privilege entries involved.
[X] Wrong: "Revoking privileges happens instantly no matter how many users or privileges exist."
[OK] Correct: The system must find and update privilege records, so more entries mean more work and more time.
Understanding how privilege changes scale helps you manage database security efficiently and shows you grasp how databases handle user permissions behind the scenes.
"What if we revoke privileges from multiple users at once? How would the time complexity change?"
Practice
REVOKE statement do in MySQL?Solution
Step 1: Understand the purpose of REVOKE
The REVOKE statement is used to remove privileges that were previously granted to a user.Step 2: Compare with other statements
GRANT adds privileges, CREATE USER creates users, and DROP TABLE deletes tables, so they do not remove privileges.Final Answer:
It removes specific privileges from a user. -> Option AQuick Check:
REVOKE = Remove privileges [OK]
- Confusing REVOKE with GRANT
- Thinking REVOKE creates users
- Assuming REVOKE deletes tables
employees table from user 'john'@'localhost'?Solution
Step 1: Understand the syntax for REVOKE on a table
The correct syntax requires specifying the database and table asdatabase.table.Step 2: Analyze each option
REVOKE SELECT ON database.employees FROM 'john'@'localhost'; correctly usesdatabase.employeesformat. Options A and C miss the database name, and B incorrectly adds .* after table name.Final Answer:
REVOKE SELECT ON database.employees FROM 'john'@'localhost'; -> Option AQuick Check:
REVOKE syntax needs database.table [OK]
- Omitting database name
- Using .* after table name incorrectly
- Wrong user specification format
GRANT SELECT, INSERT ON sales.orders TO 'anna'@'localhost'; REVOKE INSERT ON sales.orders FROM 'anna'@'localhost'; SHOW GRANTS FOR 'anna'@'localhost';
What privileges will
'anna'@'localhost' have on sales.orders after these commands?Solution
Step 1: Analyze the GRANT statement
The user 'anna' is granted SELECT and INSERT privileges on sales.orders.Step 2: Analyze the REVOKE statement
The INSERT privilege is revoked from 'anna' on sales.orders, leaving only SELECT.Final Answer:
Only SELECT privilege -> Option CQuick Check:
REVOKE removes INSERT, SELECT remains [OK]
- Assuming REVOKE removes all privileges
- Confusing which privilege was revoked
- Ignoring SHOW GRANTS output
REVOKE UPDATE ON mydb.* FROM 'mike'@'%';
Solution
Step 1: Check user specification
'mike'@'%' is a valid user format allowing any host.Step 2: Validate privilege and ON clause
UPDATE is a valid privilege, and ON mydb.* means all tables in database mydb, which is allowed.Final Answer:
The syntax is correct; no error. -> Option DQuick Check:
REVOKE syntax with database.* is valid [OK]
- Thinking ON must specify a single table
- Confusing user host wildcard syntax
- Assuming UPDATE is invalid privilege
'lisa'@'localhost' on the finance database but keep her SELECT privilege on the finance.reports table. Which sequence of commands achieves this?Solution
Step 1: Revoke all privileges on the entire finance database
UseREVOKE ALL PRIVILEGES ON finance.*to remove all rights on all tables in finance.Step 2: Grant back SELECT privilege on finance.reports table
UseGRANT SELECT ON finance.reportsto restore only SELECT on that specific table.Final Answer:
REVOKE ALL PRIVILEGES ON finance.* FROM 'lisa'@'localhost'; GRANT SELECT ON finance.reports TO 'lisa'@'localhost'; -> Option BQuick Check:
Revoke all then grant SELECT on reports [OK]
- Revoking SELECT first removes needed privilege
- Granting SELECT on whole database instead of table
- Revoking privileges in wrong order
Ready to prove it?
Pass the test and this pattern moves unknown
