Summary: in this tutorial, you will learn how to select a MariaDB database as the current database.
Introduction to the use statement
If you connect to the MariaDB server without explicitly specifying a particular database, you need to select a database as the current database to work with.
To select a specific database, you issue the use
statement as follows:
use database_name;
Code language: SQL (Structured Query Language) (sql)
In this syntax, you specify the name of the database after the use
keyword.
The use
statement instructs MariaDB to use the database_name
as the current database for the subsequent statements.
If you issue a query without selecting a database, you will receive the following error:
ERROR 1046 (3D000): No database selected
Code language: SQL (Structured Query Language) (sql)
Select a database using the MySQL command-line program
First, connect to the MariaDB server using the mysql client program:
mysql -u root -p
Enter password: ********
Code language: SQL (Structured Query Language) (sql)
Second, show all available databases in the server using the show databases
statement:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| nation |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)
Third, select the nation
database by using the use
statement:
mysql> use nations;
Database changed
Code language: SQL (Structured Query Language) (sql)
Starting from now, all the queries that you issue will execute in the context of the nation
database until the end of the session or another use
statement is issued.
For example, you can use the show tables
statement to display all tables:
mysql> show tables;
+-------------------+
| Tables_in_nation |
+-------------------+
| continents |
| countries |
| country_languages |
| country_stats |
| guests |
| languages |
| region_areas |
| regions |
| vips |
+-------------------+
9 rows in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)
To find the current database, you can use the database()
function:
mysql> select database();
+------------+
| database() |
+------------+
| nation |
+------------+
1 row in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)
In this tutorial, you have learned how to select a MariaDB database as the current database by using the use
statement