MariaDB – Create a new MySQL Database

In this article we’re going to create a new database and then create a table inside the new database. We’ll call this database ‘online_shop’. First we create a new mysql session:

[root@mariadb-client ~]# mysql -u root -p'rootpassword' -h mariadb-server.example.com 

Then let’s take a look at what dbs currently exist:

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

Now here’s how to create the database:

MariaDB [(none)]> CREATE DATABASE online_shop;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| online_shop        |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]>

At the moment the customers db doesnt contain any tables:

MariaDB [none]> USE online_shop
Database changed
MariaDB [online_shop]> SHOW TABLES;
Empty set (0.00 sec)

MariaDB [online_shop]>

So let’s create a table called ‘customers’ with the following four columns:

MariaDB [online_shop]> CREATE TABLE customers (userID INT, userFirstName char(25), userLastName char(25), userEmailAddress char(50));
Query OK, 0 rows affected (0.00 sec)

MariaDB [online_shop]>

Let’s confirm that this has worked:

MariaDB [online_shop]> SHOW TABLES;
+-----------------------+
| Tables_in_online_shop |
+-----------------------+
| customers             |
+-----------------------+
1 row in set (0.01 sec)

MariaDB [online_shop]> DESCRIBE customers;
+------------------+----------+------+-----+---------+-------+
| Field            | Type     | Null | Key | Default | Extra |
+------------------+----------+------+-----+---------+-------+
| userID           | int(11)  | YES  |     | NULL    |       |
| userFirstName    | char(25) | YES  |     | NULL    |       |
| userLastName     | char(25) | YES  |     | NULL    |       |
| userEmailAddress | char(50) | YES  |     | NULL    |       |
+------------------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Take the RHCSA Quiz

This article is part of our RHCSA Study guide (click on the yellow tab on the far left). By the end of this article you should be able to answer the following questions:


What is the mysql command to create new database called 'online_shop'?

CREATE DATABASE online_shop;

What is the mysql command to create a table called 'customers' with 4 columns, userID (integer), userFirstName(25chars), userLastName(25chars), and userEmailAddress(50chars) ?

CREATE TABLE customers (userID INT, userFirstName char(25), userLastName char(25), userEmailAddress char(50));

question?

answer

question?

answer

question?

answer

question?

answer

question?

answer