Create Database and Tables
Previous Page | Home Page | Next Page |
---|
Create a Database
The CREATE DATABASE statement is usedto create a database in MySQL.
Syntax
Contents |
CREATE DATABASE database_name
Example
The following example creates adatabase called "my_db":
<?php $con =mysql_connect("localhost","userid","abc123"); if (!$con) { die('Could not connect: ' .mysql_error()); } if (mysql_query("CREATE DATABASEmy_db",$con)) { echo "Database created"; } else { echo "Error creatingdatabase: " . mysql_error(); } mysql_close($con); ?> |
Create a Table
The CREATE TABLE statement is used tocreate a table in MySQL.
Syntax
CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... ) |
We have to add the CREATE TABLEstatement to the mysql_query() function to execute the command.
Example
The following example creates a tablenamed "Contacts", with four columns. The column names willbe "FirstName", "LastName" and "Phone":
<?php $con =mysql_connect("localhost","userid","abc123"); if (!$con) { die('Could not connect: ' .mysql_error()); } // Create table mysql_select_db("my_db",$con); $sql = "CREATE TABLE Contacts ( FirstNamevarchar(15), LastNamevarchar(15), Phoneint )";
mysql_query($sql,$con);
?>
|
Previous Page | Home Page | Next Page |
---|