Monday, July 27, 2015

MYSQL: How to Create and Delete Database

Before creating a database, we need first to be able to login to mysql. Here is how you can login.

mysql -u[username] -p[password] -h[hostname] -P [port]

CREATING NEW DATABASE

After you are able to login, you can list all the databases that are currently present in your MYSQL server.

 mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| performance_schema |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.01 sec)

You can then create a new database as easy as this:

create database [dbname];

Here is an example:

create database newDB;

Then you can check it by listing all your databases.

 mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| performance_schema |
| mysql              |
| test               |
| newDB              |
+--------------------+
5 rows in set (0.01 sec)


You can then start working on that particular database using below command:

use database newDB;


DELETING DATABASE;

Here is a very simple way to delete your database:

drop database [dbname];

Here is an example:

drop database newDB;

After deleting the database, you can again verify it with the SHOW DATABASES command.

 mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| performance_schema |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)


Hopefully you've learned from this very short guide. Click here to for more MYSQL guides.



No comments:

Post a Comment