MySQL is a very flexible and user-friendly Database. The USE command is used when there are multiple databases and we need to SELECT or USE one among them. We can also change to another database with this statement. Thus, the USE statement selects a specific database and then performs queries and operations on it using the inbuilt commands of MySQL.
Once we set the current database it will remain the same until the end of the session or unless we change it. In this article, we will see how to create a database and use it using the "USE " command.
MySQL - USE Statement
- The USE statement in MySQL is a simple but powerful command that allows us to switch to a different database within the same MySQL server instance.
- This statement is particularly useful when we are working with multiple databases and need to perform operations on a specific database.
Syntax:
USE database_name;
Example: If the user wants to use a database called GFG then the user will write:
USE GFG
Let's Create a database
Before using any database use the USE command which is required to first create it. To create a database in MySQL the below command is used. Here, we create a database named GFG:
CREATE DATABASE GFG;
Output:
create commandExamples of MySQL - USE Statement
We need to have admin privilege before creating any database. Once a database is created, we can check it in the list of databases with the following MySQL command:
SHOW DATABASES;
Output:
databasesAfter the user has created a database named GFG, we can use this database to perform the database operations. For that, the user has to type the USE command as follows:
USE GFG;
Output:
use commandAfter the use command the current database gets changed to a new database. Once we are done with performing the required operations on the database, if we do not want the database, We can drop it using the DROP statement:
DROP DATABASE GFG;
Output:
drop commandOnce a database is dropped, it’s removed permanently from the list and there is no possible way to retrieve the data from the table.
After using the drop command, if the user types –
USE GFG;
Output:
error in commandThe MySQL database will throw an error as the database GFG has been removed permanently from the database using the drop command.
Select Database() vs USE statement
SELECT Database() is used in MySQL to show the currently used database. This query is used when multiple databases are available with MySQL Server and we need to check which database is being currently used.
Output:
select database;However, the MySQL USE command is used to select a particular database. It changes the existing database to a new one and starts using it.
Output:
use commandConclusion
Overall, the USE
statement in MySQL is a important command for managing multiple databases within the same server instance. It allows users to switch to a specific database and perform operations on it using the inbuilt commands of MySQL. Understanding how to use the USE
statement is crucial for effectively working with MySQL databases as it enables users to easily navigate between databases and perform operations easily.
Similar Reads
SQL UPDATE Statement
In SQL, the UPDATE statement is used to modify existing records in a table. Whether you are updating a single record or multiple records at once, SQL provides the necessary functionality to make these changes. Whether you are working with a small dataset or handling large-scale databases, the UPDATE
6 min read
SQL USE Database Statement
SQL(Structured Query Language) is a standard Database language that is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, etc. It is flexible and user-friendly. In SQL, to interact with the database, the users have to type queries that have certain syntax, a
2 min read
MySQL CREATE VIEW Statement
MySQL, an open-source relational database management system, offers a variety of features to manage and manipulate data efficiently. One of these features is the CREATE VIEW statement, which allows you to create a virtual table known as a view. A view provides a way to simplify complex queries, enha
5 min read
MySQL SELECT Statement
The MySQL SELECT statement is essential for fetching data from tables. It retrieves information and stores it in a result table, often referred to as a result set. Widely used in MySQL, SELECT is a fundamental command for querying databases. This article covers the basics of SELECT syntax and explor
4 min read
SQL CASE Statement
The CASE statement in SQL is a versatile conditional expression that enables us to incorporate conditional logic directly within our queries. It allows you to return specific results based on certain conditions, enabling dynamic query outputs. Whether you need to create new columns, modify existing
4 min read
MySQL - SHOW VARIABLES Statement
MySQL is an open-source Relational Database Management System that stores data in the form of rows and tables and SQL is known as a programming language that is used to manipulate the data. We can perform many operations in an SQL server with the help of SQL programming language such as manipulating
3 min read
SQL CREATE VIEW Statement
The SQL CREATE VIEW statement is a very powerful feature in RDBMSs that allows users to create virtual tables based on the result set of a SQL query. Unlike regular tables, these views do not store data themselves rather they provide a way of dynamically retrieving and presenting data from one or ma
4 min read
MySQL INSERT INTO Statement
In MySQL, the INSERT INTO statement is essential for adding new data rows to a table in a database. This is important for setting up initial data in tables and for adding new records as needed when working with the database. Understanding how to use the INSERT INTO statement is key for managing and
6 min read
SQL SELECT IN Statement
The IN operator in SQL is used to compare a column's value against a set of values. It returns TRUE if the column's value matches any of the values in the specified list, and FALSE if there is no match. In this article, we will learn how IN operator works and provide practical examples to help you b
4 min read
PL/SQL DELETE Statement
In PL/SQL(Procedural Language/Structured Query Language), the DELETE statement is the powerful command used to remove one or more records from the database table. It is an essential part of database management and enables the users to efficiently manage and maintain the data integrity by selectively
7 min read