SQL Select Database

In this lesson, you learn how to select a database using SQL.

Why selecting a database?

Before executing a query related to a specific database, you need first to be sure that the right database is selected. Then automatically any query will be applied to it.

Be careful when you execute queries because if you execute one on the wrong database you may cause a huge loss.

Also, if no database is selected and you try to execute a query that cannot be executed without a selected database you will get an error response saying:

Error Code: 1046. No database selected

Select database in SQL

The USE statement is used to select a database.


Syntax

USE database_name;

You should replace database_name with the name of the database that you want to select.

Select database example

The following SQL statement selects a database called "demo".

Example

USE demo;
    

Output

0 row(s) affected

The output means that the statement is executed succesfully.

MySQL Workbench selected database

In MySQL Workbench, the selected database in the schemas navigator appears in bold as follows.


Change default database in MySQL Workbench

You can change the selected database by hitting right-click on the database that you want to select, then click on Set as default schemas as follows.

Tutorials

Online Tools

Sections

Tutorials
Tools
Posts