← Back to MySQL Course | Chapter 18: User Management & Security | Lesson 2 of 6

GRANT Privileges

Introduction to GRANT

A freshly created user starts with zero permissions by default — GRANT is the statement that assigns specific rights, like SELECT or INSERT, on particular databases or tables to that account.

Example: Introduction to GRANT

sql
GRANT SELECT, INSERT ON mydb.* TO 'app_user'@'localhost';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Granting All Privileges

Granting ALL PRIVILEGES hands a user complete administrative control over a database, including the ability to pass their own privileges on to other accounts — a powerful option that should be reserved for trusted admins only.

Example: Granting All Privileges

sql
GRANT ALL PRIVILEGES ON mydb.* TO 'admin_user'@'localhost';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Granting Column Level Privileges

Privileges can be scoped down to individual columns rather than an entire table, which is the right tool when a user needs to query most of a table but should never see sensitive fields like salary or phone number.

Example: Granting Column Level Privileges

sql
GRANT SELECT (id, name) ON mydb.employees TO 'app_user'@'localhost';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Checking Existing User Privileges

SHOW GRANTS lists every privilege currently assigned to a given account, making it easy to audit exactly what access a user has without digging through every GRANT statement that was ever run.

Example: Checking Existing User Privileges

sql
SHOW GRANTS FOR 'app_user'@'localhost';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Applying Granted Privileges Instantly

MySQL sometimes caches privilege information in memory, so after making permission changes it's good practice to reload the grant tables to guarantee the new permissions take effect immediately rather than on next login.

Example: Applying Granted Privileges Instantly

sql
GRANT SELECT ON mydb.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.