Create / Drop database in MySQL

Create / Drop database in MySQL

List the Database Present in MySQL

-- Login with Root User
$mysql -u root -p

--List the database present in MySQL
 mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| database_name      |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

Create the Database in MySQL

mysql> CREATE DATABASE database_name;
Example:
mysql> CREATE DATABASE test;
Query OK, 1 row affected (0.01 sec)

Error: If database already exists
ERROR 1007 (HY000): Can't create database 'test'; database exists
Solution:
CREATE DATABASE IF NOT EXISTS test;

Drop the Database in MySQL

mysql>DROP DATABASE database_name;
Example:
mysql>DROP DATABASE test;
Query OK, 0 rows affected (0.01 sec)

Error: If database does not exists
ERROR 1008 (HY000): Can't drop database 'test'; database doesn't exist
Solution:
mysql> DROP DATABASE IF EXISTS test;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.