mysqlmariadb

Leave a MariaDB database without quitting


In a MariaDB console (Windows command window), is there a way to leave a database without quitting? (and without going to another database) I tried use NULL; but that just looks for a database named "null".


Solution

  • The USE command is a non standard SQL extension (also supported in SQL server) and selects a default name space (database name).

    Without a default namespace (database) name you need to specify the database in your SQL syntax: e.g. SELECT a FROM myschema.t1.

    There is no command to unset the default database, since it doesn't make any sense. Either you want to use a default name space, or you don't want. There is also no way to unset the default database via client API, both COM_SELECT_DB and COM_CHANGE_USER protocol commands doesn't change the default database if you pass a NULL parameter.

    However client programs should always be aware if the default database changes or drops to avoid inconsistency and errors. This can be achieved e.g. by executing the statement SELECT DATABASE() (this is what the mysql command line client is doing) or in recent MySQL and MariaDB versions by analyzing the session track info which is part of the OK packet sent from server to the client.

    Example for command line client:

    MariaDB [(none)]> create schema x;
    Query OK, 1 row affected (0.00 sec)
    
    MariaDB [(none)]> use x;
    Database changed
    MariaDB [x]> drop schema x;
    Query OK, 0 rows affected (0.01 sec)
    
    MariaDB [(none)]>