Create and delete a MySQL database
What’s the matter?
For years MySQL and MariaDB have been the most popular OpenSource Database Managment Systems world wide. So it is no surprise that most of the common software support them natively.
There are actually only two large areas of databases. Relational databases (MySQL, PostgreSQL, Oracle and similar), and so-called non-relational databases, also called no-SQL databases.
MySQL and MariaDB belong to the relational databases.
We work with these databases very often and have found that most tasks can be done with a handful of commands. In this article, we will guide you through the commands of MySQL or MariaDB to create a new database.
Then we will show you how to delete the new (or any other) database.
Starting MySQL Console
First, log on to your MySQL or MariaDB server. To do this, connect to your server via SSH. On Windows, you can use the program PuTTY, on Linux, use any terminal program (like xTerm), and on MAC OS X you simply use the built-in terminal (type “terminal” in the Spotlight search and confirm with Enter).
If you are logged on to the server, you can simply log on to your MySQL or MariaDB server. If you have not yet created a new user who has the rights to create new databases, use the user ‘root’.
Log on as root user on MySQL
The registration process is very simple. Once you are logged in to your server, you simply use mysql-binary to login.
If your MySQL or MariaDB server has set a password, then you call the program as follows:
mysql -u root -p
If you have not set a password for the user root, do not use the parameter ‘-p’. If everything goes well, then you should see the following output:
root@mysql:~# mysql -u root -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 98 Server version: 5.5.47-0+deb8u1 (Debian) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Directly after logging in as an administrative user (root), you can create new databases or display existing databases.
List all Databases
If you have a freshly installed system with a new MySQL or MariaDB instance, then probably not all too many databases exist. First of all, let MySQL or MariaDB show you all existing databases with the command ‘SHOW DATABASES;’. Make sure you have the command in your SQL console with the semicolon; at the end.
mysql> SHOW DATABASES;
+--------------------+| Database |+--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) mysql>
As you see, my server is boring and empty.
Create a MySQL or MariaDB database
Creating a new database under MySQL or MariaDB is just as easy as listing the existing databases. Just use the SQL command ‘CREATE DATABASE <name>;’ to create a Database with the name <name>.
mysql> CREATE DATABASE blog;
Query OK, 1 row affected (0.00 sec)
If everything went well, you just created a database called blog. If you now list all your databases again, you can find the newly created database in the output:
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | blog | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.00 sec) mysql>
Deleting a MySQL or MariaDB database
If you delete a database directly with the command line tool, be sure to delete the correct one. Once the database is deleted, there is normally no going back.
First list all databases on your server. Use the command ‘SHOW DATABASES;’ in the mysql-console like in the example above.
Now copy the name of the database you want to delete.
To do delete a database you need the command ‘DROP DATABASE’. The syntax is similar to creating a database. ‘DROP DATABASE <name>;’, where <name> is the name of the database you want to delete.
An example could look like this:
mysql> DROP DATABASE blog; Query OK, 0 rows affected (0.01 sec) mysql>
The mysql-console is not very helpful here. If everything went correctly it will show you a simple “OK”.
Use the command ‘SHOW DATABASES;’ to double check the database was deleted.