In this tutorial, you will learn to create a database, select a database, and then use it.
What is Select in MysQL?
Select database(); basically displays the database which is currently in use.
There are multiple databases available in the MySQL server and workbench. So, to work with a particular database, we need to select it. In MySQL for selecting a database, a USE statement is used.
Creating a database in MySQL client
To create a database in Mysql run this command –
create database db_name;
Alternatively, you can follow the tutorial for creating mysql Databases here.
Selecting a database in the client
First, we will log into MySQL client by entering the following command-
mysql -u root -p
Then, enter the password for your particular MySQL client-
Enter password: *************
Now when we run Select database(); it will show the current database which is in use.
But here it is showing NULL, which means that we have not selected any database. For this, we will now be using USE statement.
Selecting a database when logging into MySQL client
We can directly select a database to work with at the time of logging into the MySQL client by using the following command-
mysql -u root -D sys -p
After logging in into the MySQL client write the following command-
After -D flag, the name of the database to be used is written which here is sys. In this way, we specified the database to be used.
In the output, the sys database is currently in use.
Selecting a database in MySQL workbench
You can set up a new connection in MySQL workbench by clicking on the plus sign next to MySQL connection. Then a dialog box will pop up. In that, specify the name of the connection in Connection Name: and below in the Default Schema: specify the database which you want to use and then click on OK.
In this tutorial, we studied Selecting a database using MySQL client and MySQL workbench.
You can refer to the official documentation of MySQL.