In MariaDB, the CREATE DATABASE command is used to create a new database. Each database in a server en­vi­ron­ment must be assigned a unique name. To avoid errors as­so­ci­at­ed 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 re­la­tion­al database man­age­ment system. This not only defines the name of this database, but also op­tion­al­ly various pa­ra­me­ters. Root or admin rights are required for the creation.

The name for the new col­lec­tion 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 ad­di­tion­al warnings. The following sections will explain how to use CREATE DB in MariaDB and what pre­cau­tions you can take.

Managed Database Services
Time-saving database services
  • En­ter­prise-grade ar­chi­tec­ture managed by experts
  • Flexible solutions tailored to your re­quire­ments
  • 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 place­hold­er “name_of_database”. All char­ac­ters of the ASCII code (American Standard Code for In­for­ma­tion In­ter­change) 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 char­ac­ters.

Here’s an example for a new database that contains in­for­ma­tion 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 man­age­ment system in our Digital Guide. Among other things, we explain the sim­i­lar­i­ties and dif­fer­ences between MariaDB and MySQL and guide you through the necessary steps for in­stalling MariaDB.

Go to Main Menu