CREATE TABLE student_details (
stu_id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
stu_name VARCHAR(255) NOT NULL,
college VARCHAR(255) NOT NULL,
email VARCHAR(255),
mobile VARCHAR(20)
)
The above example creates a table named persons with four columns stu_id, stu_name, college, emai and mobile inside the demo database.
Notice that each field name is followed by a data type declaration; this declaration specifies what type of data the column can hold, whether integer, string, date, etc.
There are a few additional constraints (also called modifiers) that are specified after the column name in the preceding SQL statement, like :
Each table should have a primary key column (in this case: the "stu_id" column). Its value must be unique for each record in the table.
<?php
// Create connection
$ftl_connect = mysqli_connect("localhost", "root", "", "ftl_db");
// Check connection
if (!$ftl_connect) {
die("Connection failed: " . mysqli_connect_error());
}
//Create a MySQL Table
$sql = "CREATE TABLE student_details (
stu_id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
stu_name VARCHAR(255) NOT NULL,
college VARCHAR(255) NOT NULL,
email VARCHAR(255),
mobile VARCHAR(20)
)";
if (mysqli_query($ftl_connect, $sql)) {
echo "Successfully created <b>student_details</b> table..!";
} else {
echo "Error : Creating table : " . mysqli_error($ftl_connect);
}
// Close Connection
mysqli_close($ftl_connect);
?>
<?php
// Create connection
$ftl_connect = mysqli_connect("localhost", "root", "", "ftl_db");
// Check connection
if ($ftl_connect->connect_error) {
die("Connection failed : " . $ftl_connect->connect_error);
}
//Create a MySQL Table
$sql = "CREATE TABLE student_details (
stu_id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
stu_name VARCHAR(255) NOT NULL,
college VARCHAR(255) NOT NULL,
email VARCHAR(255),
mobile VARCHAR(20)
)";
if ($ftl_connect->query($sql) === TRUE) {
echo "Successfully created <b>student_details</b> table..!";
} else {
echo "Error : Creating table : " . $ftl_connect->error;
}
//close Connection
$ftl_connect->close();
?>
<?php
try {
$ftl_connect = new PDO("mysql:host=localhost;dbname=ftl_db", "root", "");
// set the PDO error mode to exception
$ftl_connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Create a MySQL Table
$sql = "CREATE TABLE student_details (
stu_id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
stu_name VARCHAR(255) NOT NULL,
college VARCHAR(255) NOT NULL,
email VARCHAR(255),
mobile VARCHAR(20)
)";
// use exec() because no results are returned
$ftl_connect->exec($sql);
echo "Successfully created <b>student_details</b> table..!";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
//Close Connection
$ftl_connect = null;
?>