MySQL - Create Table

The CREATE TABLE statement is used to create a new table in MYSQL database.

MYSQL Syntax :
CREATE TABLE table_name (
    column1 data_type NOT NULL AUTO_INCREMENT,
    column2 data_type,
    column3 data_type,
   ............................,
PRIMARY KEY (column1)
);

The column parameters is the names of the columns of the table.
data_type will be varchar, integer, date, text, Enum, Float, Double  etc.
AUTO_INCREMENT will increment value by +1.
PRIMARY KEY can be unique and can not be NULL;

MYSQL Example :
CREATE TABLE tblstudent (
stud_id INT NOT NULL AUTO_INCREMENT ,
stud_name VARCHAR(70) NOT NULL ,
stud_mobile VARCHAR(11) NOT NULL ,
stud_address TEXT NOT NULL ,
Stud_birthdate DATE NOT NULL ,
PRIMARY KEY (stud_id)
) ENGINE = MyISAM;  

Explain :
As per as above mysql query, tblstudent will be created.

  • stud_id column is PRIMARY KEY.
  • Data Type for stud_id is integer.
  • stud_id field can not be null and its value is incremented by 1 when recorded is inserted in table.
  • Data Type for stud_name and stud_mobile is varchar with different size.
  • Data Type for stud_address is text.
  • Data Type for Stud_birthdate is Date.
  • Storage Engine for table tblstudent is MyISAM.
     

PHP SCRIPT :
You can create table using php code as per as below.
$hostname="localhost";
$username="root";
$password="********";
$link= mysqli_connect($hostname, $username, $password);
mysqli_select_db($link,DATABASE);
$returnval = mysqli_query($link,"CREATE TABLE wapstatu_video.tblstudent ( stud_id INT NOT NULL AUTO_INCREMENT , stud_name VARCHAR(70) NOT NULL , stud_mobile VARCHAR(11) NOT NULL , stud_address TEXT NOT NULL , Stud_birthdate DATE NOT NULL , PRIMARY KEY (stud_id))");
mysqli_close($link);

SHOW TABLES; statement is used to show list of tables in database.

Comments

Leave a Reply

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

64123