Creation of table in MySQL Databases
Prior to entering data in rows in a table, definition of tables is essential with appropriate names -
The following is the MySQL query to create a table -
<?php
// Make a MySQL Connection
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
// Create a MySQL table in the selected database
mysql_query("CREATE TABLE example(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(30),
age INT)")
or die(mysql_error());
echo "Table Created!";
?>
Explanation for above -
> mysql_query - Create new table
> Example refers to table name
> Descriptive Names is preferred
> Clear names mentions the content of table.
> "id" - Automatic addition of entry in the table.
> INT refers to integer
> NOT NULL refers that columns cannot be Null
> AUTO_INCREMENT - Auto Increment by 1, everytime a new entry is added.
> PRIMARY KEY - Unique Identification of rows.
> 'name VARCHAR(30),' - name(Column name) , VARCHAR{variable character(letters/numbers)}
> 'age INT,' - Stores an Integer
> 'or die(mysql_error());' - Error printing in the process of creation.
|