How to List, Create, Drop, and Rename Databases in MariaDB

In MariaDB, SHOW DATABASES lists databases, CREATE DATABASE creates one, and DROP DATABASE removes it. MariaDB has no RENAME DATABASE statement. To move to a new name, create a database with that name and restore a dump of the original into it.

List databases on the connected server

Connect to a practice server in the MariaDB client and run SHOW DATABASES. An ordinary account may only see databases on which it has privileges. Use SELECT DATABASE() in the same connection to check the current database.

SHOW DATABASES;
SELECT DATABASE() AS current_db;

If you have not selected a default database, current_db returns NULL. Use fresh names for the exercise. If a name is in use, choose a new one and substitute it consistently in the following commands.

Create and select a database

Use CREATE DATABASE to create cf_db_lesson with the utf8mb4 character set and utf8mb4_unicode_ci collation. Your account needs the CREATE privilege on the database. The second statement uses IF NOT EXISTS to show what happens when the name is already present.

CREATE DATABASE cf_db_lesson
  CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE DATABASE IF NOT EXISTS cf_db_lesson
  CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
SHOW WARNINGS;

SHOW CREATE DATABASE cf_db_lesson;
USE cf_db_lesson;
SELECT DATABASE() AS current_db;

IF NOT EXISTS leaves the existing definition intact. SHOW WARNINGS can show note 1007. SHOW CREATE DATABASE displays the stored definition, and current_db becomes cf_db_lesson after USE. See the MariaDB lesson on database creation and character sets for more on character sets and collations.

To pass a name with periods as one identifier, put the whole name cf.db.lesson inside backticks. Lowercase letters, digits, and underscores make names easier to use in scripts across systems.

CREATE DATABASE `cf.db.lesson`;
SHOW DATABASES LIKE 'cf.db.lesson';

Restore a dump under a new name

The MariaDB guide to renaming databases describes several ways to move a database. This exercise uses a dump and restore. Create an InnoDB table with two sample rows in the source and an empty destination named cf_db_lesson_new with the same character set and collation.

CREATE TABLE cf_db_lesson.items (
  id INT PRIMARY KEY,
  label VARCHAR(40) NOT NULL
) ENGINE=InnoDB;

INSERT INTO cf_db_lesson.items (id, label)
VALUES (1, 'Notebook'), (2, 'Pencil');

CREATE DATABASE cf_db_lesson_new
  CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Run the following commands in a shell from a directory that does not contain cf_db_lesson.sql. Replace db_operator with an account that can read the source and restore into the destination. The -p option prompts for the password. The --single-transaction option of mariadb-dump is suitable for the InnoDB table in this exercise.

mariadb-dump -u db_operator -p --single-transaction --result-file=cf_db_lesson.sql cf_db_lesson
mariadb -u db_operator -p cf_db_lesson_new < cf_db_lesson.sql

Omit --databases so the single-database dump does not select or recreate the source database. The second command explicitly names the destination. In PowerShell, connect with mariadb -u db_operator -p cf_db_lesson_new and run SOURCE cf_db_lesson.sql from the MariaDB prompt instead of using input redirection.

Verify the source and destination data

With an account that can read both databases, compare the table list and row counts. Check representative rows too, since equal counts cannot detect changed values.

SHOW TABLES FROM cf_db_lesson_new;

SELECT
  (SELECT COUNT(*) FROM cf_db_lesson.items) AS source_rows,
  (SELECT COUNT(*) FROM cf_db_lesson_new.items) AS target_rows;

SELECT id, label
FROM cf_db_lesson_new.items
ORDER BY id;

The destination contains items and both counts are 2. Its rows are 1, Notebook and 2, Pencil. In an application migration, check views, triggers, routines, events, privileges, and any configuration that names the database. Keep the original until those checks and the application’s queries succeed.

Drop the practice database

DROP DATABASE removes the database and its tables. IF EXISTS prevents the statement from failing when the name is absent. This example drops the empty practice database created earlier.

DROP DATABASE IF EXISTS `cf.db.lesson`;
SHOW DATABASES LIKE 'cf.db.lesson';

The final list query should return no row for cf.db.lesson. Before dropping a database you use, verify the connected server, the exact name, and that a usable backup exists.