What if you could control who sees or changes your data with just one simple command?
Why Granting privileges in MySQL? - Purpose & Use Cases
Imagine you run a small library and you want to let your friends borrow books. You write down who can borrow which books on paper. Every time someone new wants to borrow, you have to update your notes manually.
This manual method is slow and confusing. You might forget to update the notes or give someone access to books they shouldn't have. It's hard to keep track and easy to make mistakes.
Granting privileges in a database lets you easily control who can do what. Instead of writing notes, you tell the database who can read, write, or change data. It keeps everything clear and safe.
Keep a notebook: "Alice can read books A and B; Bob can borrow book C"GRANT SELECT ON library.books TO 'alice'; GRANT INSERT ON library.books TO 'bob';
This lets you quickly and safely manage access, so the right people can do the right things without confusion or risk.
A company database where only HR staff can see employee salaries, while sales staff can only see customer orders.
Manual access control is slow and error-prone.
Granting privileges automates and secures who can do what.
This keeps data safe and access clear for everyone.
Practice
GRANT SELECT ON database.* TO 'user'@'host'; command do in MySQL?Solution
Step 1: Understand the GRANT command
The GRANT command is used to give specific permissions to a user on a database or table.Step 2: Analyze the SELECT privilege
SELECT privilege allows reading data but not modifying it.Final Answer:
Allows the user to read data from all tables in the specified database. -> Option AQuick Check:
GRANT SELECT = read permission [OK]
- Confusing SELECT with UPDATE or DELETE privileges
- Thinking GRANT creates a user
- Assuming it deletes data
shop database to user 'alice' connecting from any host?Solution
Step 1: Check the database and user format
The database must be followed by .* to specify all tables, and user must include host.Step 2: Verify the correct order of clauses
The syntax is: GRANT privileges ON database.* TO 'user'@'host';Final Answer:
GRANT ALL PRIVILEGES ON shop.* TO 'alice'@'%'; -> Option AQuick Check:
Correct syntax = GRANT ALL PRIVILEGES ON shop.* TO 'alice'@'%'; [OK]
- Omitting .* after database name
- Placing TO clause incorrectly
- Not specifying host with user
GRANT SELECT, INSERT ON sales.* TO 'bob'@'localhost'; SHOW GRANTS FOR 'bob'@'localhost';
What will the
SHOW GRANTS output include?Solution
Step 1: Understand the GRANT command given
The user 'bob' is granted SELECT and INSERT privileges on all tables in the sales database.Step 2: Interpret SHOW GRANTS output
SHOW GRANTS displays the exact privileges granted, so it will list SELECT and INSERT on sales.* for bob.Final Answer:
GRANT SELECT, INSERT ON sales.* TO 'bob'@'localhost' -> Option CQuick Check:
SHOW GRANTS matches granted privileges [OK]
- Assuming ALL PRIVILEGES are granted
- Ignoring INSERT privilege
- Thinking SHOW GRANTS returns no rows
GRANT UPDATE, DELETE ON * TO 'carol'@'localhost';
Solution
Step 1: Check the ON clause syntax
The ON clause requires a database or table name, e.g., database.* or database.table.Step 2: Identify the missing part
The command uses * alone, which is invalid syntax in MySQL for ON.Final Answer:
Missing database or table name after ON -> Option DQuick Check:
ON must specify database or table [OK]
- Using * alone without database name
- Using wrong quotes for user
- Assuming localhost is invalid host
'dave' to create new tables only in the inventory database but not modify data. Which GRANT command is correct?Solution
Step 1: Identify the needed privilege
To create tables, the CREATE privilege is required; INSERT or ALL PRIVILEGES would allow data changes.Step 2: Scope the privilege to the inventory database
Granting on inventory.* limits the permission to that database only.Step 3: Check user and host format
User 'dave'@'localhost' is correctly specified.Final Answer:
GRANT CREATE ON inventory.* TO 'dave'@'localhost'; -> Option BQuick Check:
CREATE only on inventory = GRANT CREATE ON inventory.* TO 'dave'@'localhost'; [OK]
- Granting ALL PRIVILEGES instead of just CREATE
- Including INSERT which allows data changes
- Granting on *.* instead of specific database
Ready to prove it?
Pass the test and this pattern moves unknown
