Example HTML page

MySQL – Creating database

We can create a new database by using ‘Create database databasename’ command. There are two conditions “IF EXISTS” or “IF NOT EXISTS” for us to apply which are optional.

The following are the steps to create new database.

  1. Firstly we must log in to the MySQL server and type root as a password.
  2. Before going to create a new database we have to check whether the database we are going to create is already exists or not. For this we use “show databases;” command. It displays the list of existing databases.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| captcha            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

  1. Now we can create a new database as follows:
mysql> create database sports;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| captcha            |
| mysql              |
| performance_schema |
| sports             |
| test               |
+--------------------+
6 rows in set (0.00 sec)

To display the created database we use show create database command.

mysql> show create database sports;
+----------+-------------------------------------------------------------------+

| Database | Create Database                                                   |

+----------+-------------------------------------------------------------------+

| sports   | CREATE DATABASE `sports` /*!40100 DEFAULT CHARACTER SET latin1 */ |

+----------+-------------------------------------------------------------------+

1 row in set (0.00 sec)

We can create a new database using if not exists condition;

mysql> create database if not exists movies;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| captcha            |
| movies             |
| mysql              |
| performance_schema |
| sports             |
| test               |
+--------------------+
7 rows in set (0.00 sec)

What happens when we try to create a database which is already exists by using if not exists condition.

mysql> create database if not exists sports;
Query OK, 1 row affected, 1 warning (0.00 sec)

It displays warning because the database ‘sports’ is already existed.

Click here

To watch this tutorial post on YouTube Channel.

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest