🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In this chapter, we will learn how to select a specific database to work with using the USE statement in SQL. Selecting a database is essential before performing any operations on its tables and data. This chapter will guide you through the syntax and provide examples to help you understand how to select a database.
Selecting a Database
The USE statement is used to select a specific database to work with. Once you select a database, all subsequent SQL commands will be executed within that database context.
Syntax
USE database_name;
database_name: The name of the database you want to select.
Example
Assume we have a database named mydatabase:
USE mydatabase;
This command selects the mydatabase database for use.
Step-by-Step Example
Let's go through a complete example where we show how to select a database and perform operations within it.
Step-by-Step
- List All Databases:
SHOW DATABASES;
- Select the Desired Database:
USE mydatabase;
- Verify the Selection:
You can verify the selection by performing a simple query, such as listing tables:
SHOW TABLES;
Full Example Script
-- Step 1: List All Databases
SHOW DATABASES;
-- Step 2: Select the Desired Database
USE mydatabase;
-- Step 3: Verify the Selection by Listing Tables
SHOW TABLES;
Output Example
Assume mydatabase contains two tables: employees and departments.
-- Step 1: List All Databases
SHOW DATABASES;
Output:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| mydatabase |
+--------------------+
-- Step 2: Select the Desired Database
USE mydatabase;
-- Step 3: Verify the Selection by Listing Tables
SHOW TABLES;
Output:
+------------------+
| Tables_in_mydatabase |
+------------------+
| employees |
| departments |
+------------------+
Practical Use Case
Imagine you are managing multiple databases, and you need to switch between them frequently. The USE statement simplifies this process, allowing you to quickly select the database you want to work with.
Example Scenario
- Select Database for Employee Management:
USE employee_db;
- Perform Operations in
employee_db:
SELECT * FROM employees;
- Switch to Another Database for Sales Data:
USE sales_db;
- Perform Operations in
sales_db:
SELECT * FROM sales;
Conclusion
Selecting the correct database is a fundamental step before performing any SQL operations. The USE statement makes it easy to switch between different databases, ensuring that your commands are executed in the right context. By mastering the USE statement, you can efficiently manage multiple databases within your SQL environment.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment