Creating a Database Using the Command Line
Perhaps the most straightforward way to create a new database is to use the MySQL client directly.
Open a terminal window and start the MySQL client:
mysql -u root -p
Press Enter.
Usually, you will be prompted to enter the password. In LabEx VM, you don't need to enter a password, so just press Enter.
Listing Available Databases
Before we create a database, if you would like to list the available databases on your MySQL server, you can do so using the show databases;
command. Try it now (remember, it's 'database++s++', as in plural).
SHOW DATABASES;
or show databases;
are all valid commands. In SQL, commands are not case-sensitive.
SHOW DATABASES;
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.000 sec)
As you can see, the command has listed all the databases available on the server.
Now, back to creating databases.
Creating a Database Using the MySQL client
You can use the create database
command to accomplish this task. Try it now and then use the show databases
command again to see the difference.
CREATE DATABASE Hello_World;
SHOW DATABASES;
MariaDB [(none)]> CREATE DATABASE Hello_World;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| Hello_World |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.000 sec)
As you can see, you have successfully created a new database called 'Hello_World'.
Case-Sensitivity of Database Names
While SQL itself is a case-insensitive language, in MySQL, database names are case-sensitive, as well as table names. To demonstrate, make the following database.
CREATE DATABASE Hello_World;
MariaDB [(none)]> CREATE DATABASE Hello_World;
ERROR 1007 (HY000): Can't create database 'Hello_World'; database exists
As you can see, it failed because there is already a database called 'Hello_World'.
However, try to create another database as follows.
CREATE DATABASE hello_world;
SHOW DATABASES;
MariaDB [(none)]> CREATE DATABASE hello_world;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| Hello_World |
| hello_world |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.000 sec)
As you can see, we were successful, because database names are case-sensitive. 'Hello_World' and 'hello_world' are two different databases.