Learn how to select a Database using the SQL USE database. If you want to create a table in a database and you have many databases on your MySQL server. You need to specify the database where you want to create the table.

To indicate a database you have to use the SQL USE database statement.


Replace the databasename with the name of the database you want to use and execute the query for the required database.

SQL USE DATABASE Statement Example

The below example contains the database name ‘mydb’. If you have a query and want to execute the query for the particular database, then use this example.

Selecting a database for running your query is the required task when you have not selected any database. Before executing any query you must specify which database you want to use.

You can replace the above database example name with your one.

Execute Query for Selected Database Using SQL USE DATABASE Statement

Below is the SQL CREATE TABLE statement to create a table for a database. The first line in the below example selects the database for the query.

The second line creates a table called ‘test’ with some data fields.

Use this method for executing any query by selecting a database before execution.

You must also read.

MySQL doc