0
0
MysqlHow-ToBeginner · 3 min read

How to Check Current Database in MySQL Quickly

To check the current database in MySQL, use the SELECT DATABASE(); command. It returns the name of the database you are currently connected to or NULL if none is selected.
📐

Syntax

The command to check the current database is simple:

  • SELECT DATABASE(); - Returns the name of the current database.

If no database is selected, it returns NULL.

sql
SELECT DATABASE();
💻

Example

This example shows how to check the current database after connecting to MySQL and selecting a database.

sql
USE employees;
SELECT DATABASE();
Output
DATABASE() employees
⚠️

Common Pitfalls

Sometimes users forget to select a database before running queries. If you run SELECT DATABASE(); without selecting a database, it returns NULL.

Wrong way:

SELECT DATABASE();

Output:

DATABASE()
NULL

Right way:

USE your_database_name;
SELECT DATABASE();
📊

Quick Reference

CommandDescription
SELECT DATABASE();Shows the current database name or NULL if none selected
USE database_name;Selects a database to use for queries

Key Takeaways

Use SELECT DATABASE(); to find the current database in MySQL.
If no database is selected, SELECT DATABASE(); returns NULL.
Always select a database with USE before running queries.
Checking the current database helps avoid running queries on the wrong database.