How to use MariaDB CREATE DATABASE

In MariaDB, the CREATE DATABASE command is used to create a new database. Each database in a server environment must be assigned a unique name. To avoid errors associated with duplicate database names, there are two optional clauses that you can use with this MariaDB command.

What is CREATE DATABASE used for in MariaDB?

The CREATE DATABASE command in MariaDB is used to create a new database within the free and relational database management system. This not only defines the name of this database, but also optionally various parameters. Root or admin rights are required for the creation.

The name for the new collection must be unique within the server structure. If you try to use a name that is already in use, you will receive an error message without any additional warnings. The following sections will explain how to use CREATE DB in MariaDB and what precautions you can take.

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

What is the syntax for CREATE DATABASE?

The basic syntax of CREATE DATABASE in MariaDB is:

CREATE DATABASE name_of_database;
sql

With this command, you create a new database. You specify the name of the database in the placeholder “name_of_database”. All characters of the ASCII code (American Standard Code for Information Interchange) are permitted for database names. This includes all letters of the Latin alphabet in upper and lower case, numbers from 0 to 9 and numerous special characters.

Here’s an example for a new database that contains information about customers:

CREATE DATABASE customer_list_2024;
sql

Once you’ve created a database, you can add new users with MariaDB CREATE USER and create new tables with MariaDB CREATE TABLE.

CREATE DATABASE with OR REPLACE

Two optional clauses can be added to the syntax of CREATE DATABASE for MariaDB, both of which help to prevent an error message from appearing if a database with the same name already exists. The first clause is called OR REPLACE and is used to replace a database if it has the same name. Here’s the syntax:

CREATE OR REPLACE DATABASE name_of_database;
sql

The notation above is basically a condensed version of this code:

DROP DATABASE IF EXISTS name_of_database;
CREATE DATABASE name_of_database;
sql

OR REPLACE has been supported since version 10.1.3.

CREATE DATABASE with IF NOT EXISTS

The second optional clause for CREATE DB under MariaDB is IF NOT EXISTS. The is used to check whether a database with the same name already exists. If one doesn’t exist, the database is created. However, if a database with the same name already exists, you’ll receive a warning message instead of an error message, and the database will not be created. The syntax for this clause looks like this:

CREATE DATABASE IF NOT EXISTS name_of_database;
sql
Tip

You can find out more about the open-source database management system in our Digital Guide. Among other things, we explain the similarities and differences between MariaDB and MySQL and guide you through the necessary steps for installing 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