Virtual Open Schooling Online courses available at Virtual Open Schooling (VOS), for more details kindly click here!
NIOS Silver Jubilee Year
NIOS

Create Database and Tables

From NIOS
Jump to: navigation, search
Previous Page Home Page Next Page
ICT Applications


Create a Database

The CREATE DATABASE statement is usedto create a database in MySQL.

Syntax

CREATE DATABASE database_name

Example

The following example creates adatabase called "my_db":


<?php

$con =mysql_connect("localhost","userid","abc123");

if (!$con)

{ die('Could not connect: ' .mysql_error()); }

if (mysql_query("CREATE DATABASEmy_db",$con))

{ echo "Database created"; }

else { echo "Error creatingdatabase: " . mysql_error(); }

mysql_close($con);

?>

Create a Table

The CREATE TABLE statement is used tocreate a table in MySQL.


Syntax


CREATE TABLE table_name

(

column_name1 data_type,

column_name2 data_type,

column_name3 data_type,

....

)

We have to add the CREATE TABLEstatement to the mysql_query() function to execute the command.


Example


The following example creates a tablenamed "Contacts", with four columns. The column names willbe "FirstName", "LastName" and "Phone":


<?php

$con =mysql_connect("localhost","userid","abc123");

if (!$con)

{

die('Could not connect: ' .mysql_error());

}

// Create table

mysql_select_db("my_db",$con);

$sql = "CREATE TABLE Contacts

(

FirstNamevarchar(15),

LastNamevarchar(15),

Phoneint

)";


// Execute query

mysql_query($sql,$con);


mysql_close($con);

?>


Previous Page Home Page Next Page
ICT Applications