MySQL - Database


In this tutorial we will learn to work with databases in MySQL.

Open the terminal and login to your MySQL server by using the following command.

$ mysql -u root -p

What is Database?

Database is a storage space and it consists of multiple tables that holds our data.

Show Databases

The first command we will learn is the SHOW DATABASES; command and it is used to show all the databases living in the MySQL server.

Even though we have not yet created any database but still there will be few databases inside the MySQL server which got created at the time of MySQL server installation.

| Database           |
| information_schema |
| mysql              |
| performance_schema |
3 rows in set (0.00 sec)

Commands in MySQL terminal ends with ; semicolon.

Create database

To create a database we use the following command.

CREATE DATABASE database_name;

So, let us go ahead and create a mysql_project database.

mysql> CREATE DATABASE mysql_project;
Query OK, 1 row affected (0.00 sec)

We can use both capital letters and small letters for the MySQL keywords. Like CREATE DATABASE db_name; or create database db_name;.

Now if we check the databases using the show databases; command we will get the following output.

mysql> show databases;
| Database           |
| information_schema |
| mysql              |
| mysql_project      |
| performance_schema |
4 rows in set (0.01 sec)

Use database

After creating a database we have use the USE db_name; command to start working inside the database.

In the following example we will use the mysql_project database.

mysql> USE mysql_project;
Database changed

Drop database

We use the DROP DATABASE db_name; command to drop or delete a database from MySQL.

In the following example we are dropping/deleting the db_1 database.

mysql> DROP DATABASE db_1;
Query OK, 0 rows affected (0.01 sec)

In the next tutorial we will learn to create tables.