Breaking

LightBlog

Saturday 4 July 2015

Create a Table in MySQL Database-PHP


A Database is collection of Table. A Table is collection of Row and Column in which we store the information. You can create a tables using SQL using Create Table Command. Each table has set of field. Which each field has some data type. Each table has unique name.
Syntax:
               Create table table-name (field name1 data type( size), field name2 data type( size));
 Example:
               Create table stu (name char (20), roll_no int(10));

The table creation command requires:
  • Name of table
  • Name of fields
  • Definition for each field 

Create table emp ( emp_id int not null AUTO_INCREMENT, Name varchar( 50), PRIMERY KEY(emp_id));
Here few items need explanation:
  • Field attribute NOT NULL is being used because we do not want this field to be NULL.
  • Field attribute AUTO_INCREMENT tells MySQL to go ahead and add the next value to current number.
  • Keyword PRIMARY KEY is used to define a column as primary key and value cannot repeat in column.

                                               
                              
Example
<?php
$con=mysql_connect(“localhost”, “deep “, 12345);
if (!$con)
{
die(“could not be connect the database” . mysql_error());
}
echo “ connected successfully “;
$sql = “Create table emp ( “emp_id int not null AUTO_INCREMENT , “. “Name varchar( 50)”,. “PRIMERY KEY(emp_id)); “;
mysql_select_db (‘employee’);
$retrive = mysql_query ($sql , $con  );
mysql_close ($con);
?>

 
 
                                               
Adbox