Select Database in MySQL

Introduction

After creating a database, the next step is to select or use that database so you can perform operations like creating tables, inserting data, and running queries.

In MySQL, selecting a database means telling the system which database you want to work with.


What is SELECT DATABASE (USE)

The USE statement is used to select a database in MySQL.

Once a database is selected:

  • All SQL operations will be performed on that database

  • You can create tables and manage data inside them


Basic Syntax

USE database_name; 

Example

USE School; 

This command selects the School database.

After this, any table you create will be inside the School database.


Why Selecting a Database is Important

Before running queries like CREATE TABLE or INSERT, you must select a database.

If you do not select a database, MySQL may show an error like:

“No database selected”


Verify Selected Database

To check which database is currently selected, use:

SELECT DATABASE(); 

This will display the name of the active database.


Example Scenario

Suppose you have created a database:

CREATE DATABASE student_db; 

Before creating tables, you must select them:

USE student_db; 

Then you can create tables:

CREATE TABLE Students ( id INT, name VARCHAR(50) ); 

Switching Between Databases

You can switch between databases anytime using the USE command.

Example:

USE school_db; USE company_db; 

This allows you to work with multiple databases easily.


Common Mistakes

  • Forgetting to select a database before running queries

  • Typing the wrong database name

  • Assuming a database is already selected


Key Points to Remember

  • USE statement is used to select a database

  • It sets the active database for all operations

  • Always select a database before creating tables

  • Use SELECT DATABASE() to verify the current database