MySQL Select Database

MySQL does not preselect a database for you. Every time we open a new session, we have to specify which database to use.

The way to select a database in MySQL is by using the USE command.

The steps to selecting a database are as follows:

Select a database in MySQL using the command line

  • Open the MySQL Command Line with any user's credentials.
  • To see the databases that are already present, we can use the "show databases" command.
show databases;

To select a database, we type

USE <database_name>;

Example: USE test;

If we use a database name that does not exist, MySQL throws an error.

Using MySQL Workbench

To select a database in MySQL workbench, we do the following:

  • Establish a new connection using the MySQL workbench.
  • Open a new SQL script.
  •  Type the command
show databases;
  • Select the database you require and type:
USE <database_name>;

Example: USE test;

The image below all the steps in sequence: