What if you could instantly stop someone from accessing your data with just one command?
Why Revoking privileges in MySQL? - Purpose & Use Cases
Imagine you run a busy library where many people borrow books. You give some friends permission to lend books to others. But later, you realize some friends should no longer have that power. Without a clear way to take back permissions, you have to watch everyone closely or manually check each transaction.
Manually tracking who can do what is slow and confusing. Mistakes happen easily, like forgetting to stop someone from lending books or accidentally letting someone keep too much control. This can cause chaos and security risks.
Revoking privileges lets you quickly and safely take back permissions from users. It is like having a simple switch to turn off access, so you keep control without hassle or errors.
Keep a list on paper and check it every time someone borrows a book.REVOKE SELECT ON database.* FROM 'user'@'host';
It enables you to manage who can do what in your database easily and securely, protecting your data and keeping control.
A company hires a contractor and gives them access to some data. When the project ends, the company revokes the contractor's privileges to keep the data safe.
Manual permission tracking is slow and error-prone.
Revoking privileges quickly removes unwanted access.
This keeps your database secure and under control.
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
