How to choose a database using SELECT DATABASE in MariaDB

Once you have connected to the open-source database management system, you need to select the database you want to work with in MariaDB. You have two options for this: you can use the USE command in the MySQL command line or the mysql_select_db function in PHP. We will cover both methods.

The command USE in the command line

The syntax of USE is as follows:

USE name_of_database;
sql

You must always use the command in combination with a special database and use this instead of the placeholder “name_of_database”. If you omit this parameter, you will receive an error message (ERROR 1046).

To make it easier for you to understand how this works, we will use a simple example. Let’s imagine that we want to access the “Customers” database. The following steps are necessary:

  1. Log in to your server via the command line:
mysql -u root -p
Enter password: ************
sql
  1. Use the SHOW DATABASES command to get an overview of all available databases on your server:
mysql> SHOW DATABASES;
sql
  1. To select the desired database, use the command USE:
mysql> USE customers;
sql

Now you can work in the database and create a new table with MariaDB CREATE TABLE. If the desired database is not yet listed, create it with the MariaDB command CREATE DATABASE. If a database is no longer required, remove it with the MariaDB command DROP DATABASE.

Managed Database Services
Time-saving database services
  • Enterprise-grade architecture managed by experts
  • Flexible solutions tailored to your requirements
  • Leading security in ISO-certified data centers

SELECT DATABASE for MariaDB in PHP

The function SELECT DATABASE for MariaDB can also be found in PHP (here: mysqli_select_db). The syntax for establishing the connection is as follows:

$connection = mysqli_connect("server", "username", "password");
php

To select the database, the subsequent command looks like this:

mysqli_select_db($connection, "customer");
php
Tip

You can find many more articles about the open-source database management system in our Digital Guide. Here, for example, we explain the differences and similarities between MariaDB and MySQL and how to install MariaDB.

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top