php-mysql

Create Database and Tables in PHP MySQL

As We know database holds one or multiple tables. Here in this post you will learn how create database tables in Mysql

Create a Database

We use the CREATE DATABASE statement to create a database in MySQL.

Syntax

CREATE DATABASE database_name

To get PHP to execute the MYSQL statement we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

For Example

The following example creates a database called “school_database”:

<?php

$con = mysql_connect(“localhost”,”username”,”password”);

if (!$con)

{

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

}

if (mysql_query(“CREATE DATABASE school_database”,$con))

{

echo “Database created”;

}

else

{

echo “There is a error creating database: ” . mysql_error();

}

mysql_close($con);

?>

Create a Table

We use the CREATE TABLE statement to create a table in MySQL.

Syntax

CREATE TABLE table_name

(

column_name1 data_type,

column_name2 data_type,

column_name3 data_type,

….

)

We must add the CREATE TABLE statement to the mysql_query() function to execute the command.

For Example

The following example creates a table named “student”, with three columns. The column names will be “FirstName”, “LastName” and “Age”:

<?php

$con = mysql_connect(“localhost”,”username”,”password”);

if (!$con)

{

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

}

// Create database

if (mysql_query(“CREATE DATABASE school_database”,$con))

{

echo “Database created”;

}

else

{

echo “There is a error creating database: ” . mysql_error();

}

// Create table

mysql_select_db(“school_database”, $con);

$sql = “CREATE TABLE student

(

FirstName varchar(15),

LastName varchar(15),

Age int

)”;

// Execute query

mysql_query($sql,$con);

mysql_close($con);

?>

Remember to create a table via php, first we will select database. The database is selected with the mysql_select_db() function.

When you create a database field of type varchar, you must specify the maximum length of the field, e.g. varchar(10).

Each table should have a primary key field.

A primary key is used to uniquely identify the rows in a table. Each primary key value must be unique within the table. And the primary key field cannot be null because the database engine requires a value to locate the record.

The following example sets the stuID field as the primary key field. The primary key field is often an ID number, and is often used with the AUTO_INCREMENT setting. AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added. To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field.

For Example

$sql = “CREATE TABLE student

(

stuID int NOT NULL AUTO_INCREMENT,

PRIMARY KEY(personID),

FirstName varchar(10),

LastName varchar(10),

Age int

)”;

mysql_query($sql,$con);

Related Posts


Leave a Reply

Your email address will not be published. Required fields are marked *


*