[MySQL Tutorial 3] Creating Database

2

In the previous MySQL tutorial on Connecting to MySQL, you learnt how to connect to the MySQL database. In this tutorial, you will learn how to create database in MySQL.

MySQL is a relational database system. It contains data in the form of tables and these tables are present in databases. To check which databases you have, use the following command in mysql console:

show databases;


This will result in a list of databases present in your system.

To create your own database, the syntax is as follows:

Create database database_name ;


You can create database with the keyword “create database” followed by the name of your database ending with a semicolon. For example,

Create database new;


This command will create a database with the name “new”.

You can check whether it has been created or not by “show databases;” command as follows:

The list now contains the database “new” created by us.

The databases can be deleted also by drop command as follows:

Drop database database_name;


For example,

Drop database new;

Now if you check “show databases;” command, you will see “new” database is not there in the list.

The “create” and “drop database” operations can be done using PHP program as follows:

<?php

mysql_connect('localhost','root','your_password');

if(mysql_query("create database new"))

{

                echo "database created";

}

else

{

                echo "not created";

}

?>

Save the program as “create_db.php” in C:/wamp/www and run it in your browser as “http:/localhost/create_db.php”. The output of the above code is:

You can check in MySQL console whether the database created using PHP exist or not using “show databases;” command.

Similarly, you can delete this database as follows:

<?php

mysql_connect('localhost','root','your_password');

if(mysql_query("drop database new"))

{

                echo "database deleted";

}

else

{

                echo "not deleted";

}

?>

 

The output will be:

Check using “show databases;”, the database will be deleted.

Now that you know how to create database, continue with the next tutorial on how to select database and table in MySQL. Also, if you have any queries, please leave a comment below.

Share.

2 Comments

  1. there’s an error in database deleting command
    As it must be drop database database_name rather than drop database_name

    • Thank you Naveen for notifying the error. It has been corrected.
      And thank you for reading my post.

Leave A Reply