← Back to MySQL Course | Chapter 2: Databases & Tables | Lesson 3 of 7

USE Database

USE एक particular classroom में चलने जैसा है ताकि आप आगे जो कुछ भी कहें वह उस room के बारे में हो।
Syntax
sql
USE database_name;

एक Database Select करना

एक database name के बाद USE उस database को आपकी current session के लिए active context set कर देता है, इसलिए बाद का हर unqualified table reference automatically इसके अंदर resolve होता है। इसका मतलब है कि उस session के दौरान बाद की queries में आपको हर table name के आगे database name prefix करने की ज़रूरत नहीं।

उदाहरण: Selecting a Database

sql
USE shop;
SELECT * FROM products;

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

Selected Database Check करना

SELECT DATABASE() चलाना उस database का नाम return करता है जो इस समय active है, जो यह confirm करने का एक तेज़ तरीका है कि आप सही target के against एक destructive query चलाने वाले हैं।

उदाहरण: Checking the Selected Database

sql
SELECT DATABASE();

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

Databases Switch करना

आप बिना अपना connection बंद और फिर से खोले किसी session में किसी भी point पर USE को दोबारा call करके पूरी तरह किसी दूसरी database पर switch कर सकते हैं। यह उन scripts या admin tools में खासतौर पर common है जिन्हें sequence में कई databases पर operate करना है।

उदाहरण: Switching Databases

sql
USE shop;
USE reporting;

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

बिना Select किए Tables Query करना

database_name.table_name से किसी table name को prefix करना आपको context बदले बिना databases के across query करने देता है, जो same server पर अलग schemas के बीच one-off joins के लिए handy है।

उदाहरण: Querying Tables Without Selecting

sql
SELECT * FROM shop.products;

Active Session Manage करना

एक fresh connection अक्सर बिल्कुल कोई database selected न होने के साथ शुरू होता है, इसलिए USE call करने से पहले एक bare query चलाना 'no database selected' error के साथ fail हो सकता है — हमेशा पहले अपना context confirm करें।

उदाहरण: Managing the Active Session

sql
-- A fresh connection has no database selected; a bare query first can fail
USE shop;
SELECT * FROM products;

⚠️ 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. USE shop; भूल जाना और SELECT * FROM products; चलाते समय No database selected पाना।
  2. यह मान लेना कि USE हमेशा के लिए रहता है, जबकि यह सिर्फ current connection या session पर apply होता है।
  3. यह न जानना कि कौन सी database active है, जबकि SELECT DATABASE(); इसे दिखाती है और कोई selected न होने पर NULL return करती है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.