Horje
MySQL USE Statement

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

create command

Examples 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:

show

databases

After 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

use command

After 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

drop command

Once 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

error in command

The 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

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:

northwind

use command

Conclusion

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.

FAQs on the MySQL USE Statement

What is the purpose of the MySQL USE statement?

The MySQL USE statement is used to select a specific database within a MySQL server instance. Once selected, all subsequent queries and operations are performed on the chosen database until the end of the session or until another database is selected using another USE statement.

How do you use the USE statement in MySQL?

The syntax for using the USE statement is simple:

USE database_name;

Is the USE statement session-specific?

Yes, the USE statement is session-specific. Once you set the current database using the USE statement, it remains in effect for the duration of the session unless changed again using another USE statement.




Reffered: https://www.geeksforgeeks.org


Databases

Related
How to Handle Multiple Enumeration Fields in Scala Case Classes? How to Handle Multiple Enumeration Fields in Scala Case Classes?
MongoDB/NoSQL Keeping Document Change History MongoDB/NoSQL Keeping Document Change History
How to Search by id in MongoDB How to Search by id in MongoDB
How to Retrieve only the Queried Element in an Object Array in MongoDB Collection How to Retrieve only the Queried Element in an Object Array in MongoDB Collection
How is id Generated in MongoDB How is id Generated in MongoDB

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
17