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

GRANT Privileges

GRANT permission slips बाँटते एक teacher जैसा है। एक नया user कोई भी slip के बिना शुरू होता है, और हर slip उन्हें एक specific चीज़ करने देती है।
Syntax
sql
GRANT privilege1, privilege2
ON database_name.table_name
TO 'username'@'host';

GRANT का Introduction

एक newly created user default रूप से zero permissions से शुरू होता है — GRANT वह statement है जो उस account को particular databases या tables पर SELECT या INSERT जैसे specific rights assign करता है।

उदाहरण: 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.

All Privileges Grant करना

ALL PRIVILEGES grant करना एक user को database पर complete administrative control सौंप देता है, अपने खुद के privileges दूसरे accounts को pass करने की ability सहित — एक powerful option जो सिर्फ trusted admins के लिए reserved होनी चाहिए।

उदाहरण: 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.

Column Level Privileges Grant करना

Privileges को पूरी table के बजाय individual columns तक scope किया जा सकता है, जो सही tool है जब एक user को table का ज़्यादातर हिस्सा query करना हो लेकिन salary या phone number जैसे sensitive fields कभी न देखने चाहिए हों।

उदाहरण: 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.

मौजूदा User Privileges Check करना

SHOW GRANTS किसी दिए गए account को इस समय assign हर privilege list करता है, आपको यह audit करना आसान बनाते हुए कि एक user के पास exactly क्या access है बिना कभी चलाए गए हर GRANT statement खोजे।

उदाहरण: 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.

Granted Privileges तुरंत Apply करना

MySQL कभी-कभी privilege information memory में cache करता है, इसलिए permission changes करने के बाद grant tables reload करना अच्छी practice है यह guarantee करने के लिए कि नई permissions अगले login के बजाय तुरंत effect में आएँ।

उदाहरण: 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.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक application user को ALL PRIVILEGES ON *.* grant करना, जो ज़रूरत से कहीं ज़्यादा access देता है।
  2. ON mydb.* के बजाय ON mydb लिखना, जो एक syntax error है।
  3. app_user@localhost को grant करना जब user एक अलग host से connect होता है।
🔒

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.