---
title: "Creating Database and Table’s in MySQL"  
description: "In this article I will show you, how to create database and tables within MySQl database in PHP.  Creating Database:CREATE DATABASE statement is used"  
author: "Anonymous User"  
published: 2011-09-20  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/736/creating-database-and-table-s-in-mysql  
category: "php"  
tags: ["php"]  
reading_time: 2 minutes  

---

# Creating Database and Table’s in MySQL

In this [article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370) I will show you, how to [create database](https://www.mindstick.com/articles/1623/sfdc-create-an-app-and-database) and tables within [MySQl database](https://www.mindstick.com/forum/390/how-to-create-mysql-database-backup-file-using-php) in PHP.\

##### Creating Database:

CREATE DATABASE statement is used to creating database in MySQL database server.

##### Syntax:

CREATE DATABASE DATABASE_NAME

##### Example:

```
<?php      //  creatingconnection with mysql database server      $con = mysql_connect("localhost", "root", "root");    // check connection is establish or not    if(!$con)    {        die ("Connectionnot established!  Please try again");    }      echo '<b>Connection successfully open</b>'.'</br>';      // Creating database          $qry = "CREATE DATABASE TempDatabase";    // Executing query in MySql database server     if(mysql_query($qry,$con))      {         echo 'Database successfully created.'.'</br>';     }    else     {         echo 'Database creation failed ! ';     }      //Closing the connection    mysql_close($con);    echo '<b>Connection closed successfully</b>';?>
```

##### Output:

![Creating Database and Table’s in MySQL](https://www.mindstick.com/mindstickarticle/6e284d44-26e1-4caa-bf6e-b396c67cf6f1/images/2c60ea45-b45d-4481-a034-00fe5c504410.png)

##### Creating Table:

CREATE TABLE statement is used for creating table in MySQL database server.

##### Syntax:

```
CREATETABLE Table_Name (       column_name1  data_type,       column_name2data_type,       ......................       ......................         column_nameNdata_type )
```

##### Example: Creating table in specified database

```
<?php      //  creatingconnection with mysql database server      $con = mysql_connect("localhost", "root", "root");    // check connection is establish or not    if(!$con)    {        die ("Connectionnot established!  Please try again");    }      //Selecting databse     mysql_select_db("TempDatabase",$con);    //  Creatingtable in above selecting database         $qry ="CREATE TABLE tblTemp                (                  Id int,                  Username varchar(50),                  EmailId  varchar(60),                  Address varchar(100),                  MobileNo varchar(20)                    )";    //  Executingquery in MySql database server     if(mysql_query($qry,$con))     {         echo 'Table successfully created.'.'</br>';    }     else    {         echo 'Table creation failed ! ';    }      //Closingthe connection     mysql_close($con);  ?>
```

**Note:** A database must be selected before a table can be created. The database is selected with the mysql_select_db() function. When you create a table field of type varchar, you must specify the [maximum length](https://answers.mindstick.com/qa/40973/what-is-the-maximum-length-of-a-url-in-different-browsers) of the field, e.g. varchar(50).

##### Output:

![Creating Database and Table’s in MySQL](https://www.mindstick.com/mindstickarticle/6e284d44-26e1-4caa-bf6e-b396c67cf6f1/images/1d353297-ebdb-4cc2-8baa-ad6403ac0d2f.png)

\

---

Original Source: https://www.mindstick.com/articles/736/creating-database-and-table-s-in-mysql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
