How to create MySQL table step by step guide for beginner


Mysql Create Table | In this tutorial we learn how to create MySQL table on centos/rhel/Oracle server.

Before creating mysql table we have to select three value one is table name,seconf column name and column type .

Create MySQL table using mysql promt

Suppose we are going to create a mysql table named “student”. 
Mysql create table using mysql promt we execute the below command.

create table student(
   studentnrollno INT NOT NULL AUTO_INCREMENT,
   studentfirstname VARCHAR(30) NOT NULL,
   studentlastname VARCHAR(30) NOT NULL,
   PRIMARY KEY ( studentnrollno )
);

After executing  the above command we create a mysql table called student which has three field one is studentrollno,second is student firstname and then student last name and we define student rollno  as a primary key. We use NOT NULL which pevent to entry null value and student roll no increment automatically using the AUTO_INCREMENT keyword.

To execute the above command we need to login mysql server and after getting the mysql  promt we need to execute the command as below.

mysql> create table student(
    ->    studentnrollno INT NOT NULL AUTO_INCREMENT,
    ->    studentfirstname VARCHAR(30) NOT NULL,
    ->    studentlastname VARCHAR(30) NOT NULL,
    ->    PRIMARY KEY ( studentnrollno )
    -> );
Query OK, 0 rows affected (0.02 sec)
How to create MySQL table step by step guide for beginner,mysql create table
mysql create table

 Student table has been created after executing the above command.


Create MySQL table using php script

Mysql create table by php script we are going to take mysql_query function .

To connect to mysql server we use mysql_connect function and to select the database we will use mysql_select_db function.All functions are important to create mysql table using php script. 

To create mysql table we need to pass two arguments inside the mysql_query function. 

Lets take an example to demonstrate for create mysql table using php script.
<html>
   <head>
      <title>Create MySQL table on linux system</title>
   </head>
   <body>
      <?php
         $mysql_host = 'localhost';
         $mysql_user = 'root';
         $mysql_pass = 'passwordofroot';
         $conn = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
        
         if(! $conn ) {
            die('Could not connect the server: ' . mysql_error());
         }
         echo 'Connection successfully established';
         $sql = “CREATE TABLE student( ".
            "studentnrollno INT NOT NULL AUTO_INCREMENT, ".
            "studentfirstname VARCHAR(30) NOT NULL, ".
            "studentlastname VARCHAR(30) NOT NULL, ".
            "PRIMARY KEY ( studentnrollno ));”;
          mysql_select_db( 'techrideradmin' );
         $result = mysql_query( $sql, $conn );
        
         if(! $result ) {
            die('table could not create: ' . mysql_error());
         }
         echo "Table created successfully\n";
         mysql_close($conn);
      ?>
   </body>
</html>

That’s all.If this article is helpful to know about Mysql Create Table please share it!!!!



SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment