🎓 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 database in MySQL. Selecting a database allows you to perform operations on that database, such as creating tables, inserting data, and running queries. Let's explore how to select a database and work with it.
Selecting a Database
To select a database, we use the USE statement. This command tells MySQL to switch to the specified database so that any subsequent operations are performed on it.
Syntax
USE database_name;
database_name: The name of the database you want to select.
Example
USE mydatabase;
This example selects the database named mydatabase for use.
Full Example
Let's go through a full example where we create a database, select it, and then create a table within it.
- Create a Database:
CREATE DATABASE company;
- Select the Database:
USE company;
- Create a Table:
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100)
);
- Insert Data into the Table:
INSERT INTO employees (first_name, last_name, email) VALUES ('Rahul', 'Sharma', 'rahul.sharma@example.com');
INSERT INTO employees (first_name, last_name, email) VALUES ('Priya', 'Singh', 'priya.singh@example.com');
- Select Data from the Table:
SELECT * FROM employees;
Output
| id | first_name | last_name | |
|---|---|---|---|
| 1 | Rahul | Sharma | rahul.sharma@example.com |
| 2 | Priya | Singh | priya.singh@example.com |
Checking the Current Database
To check which database is currently selected, you can use the following command:
SELECT DATABASE();
Example
SELECT DATABASE();
Output
| DATABASE() |
|---|
| company |
Conclusion
Selecting a database in MySQL is a simple but crucial step in managing your data. Once a database is selected, you can perform various operations on it, such as creating tables and inserting data. This chapter covered how to select a database and provided a full example of creating and using a database.
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