Checking the current database in MySQL or MariaDB prevents commands from running against the wrong schema when statements omit a database name. Confirming session context before commands such as SHOW TABLES, DESCRIBE orders, or data-changing statements reduces the risk of reading or modifying the wrong application data.
Each client connection keeps its own default database, and the server uses that setting whenever a statement references an unqualified table or view name. SELECT DATABASE() asks the server which database is current for the session, and NULL means the session has no default database yet.
The current database is session-scoped rather than server-wide, so it can differ between terminals, scripts, or separate client tabs. MySQL systems typically use the mysql client command, while current MariaDB packages prefer mariadb; if a session should start in a specific schema, select it after login or start the client with –database=<db_name> and then repeat the check.
$ mysql --host=db.example.net --user=dbadmin --password
On current MariaDB systems, use mariadb if that is the client installed on the host.
mysql> SELECT DATABASE(); +------------+ | DATABASE() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec)
NULL means the session does not have a default database selected yet.
mysql> USE appdb; Database changed
USE changes the default database only for the current connection.
mysql> SELECT DATABASE(); +------------+ | DATABASE() | +------------+ | appdb | +------------+ 1 row in set (0.00 sec)
To start the client in a known schema, connect with --database=appdb and then run SELECT DATABASE();.
mysql> exit Bye