Don't forget to hit the ⭐ if you like this repo.
A table organizes the information into rows and columns. The SQL CREATE TABLE
statement is used to create a table in the database. Database: demo1, table: persons.
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "demo1");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt create table query execution
$sql = "CREATE TABLE persons(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(70) NOT NULL UNIQUE
)";
if(mysqli_query($link, $sql)){
echo "Table created successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
The provided code snippet demonstrates the creation of a table named "persons" in a database using SQL. The table definition includes the following columns:
-
id
: This column is of typeINT
and serves as the primary key for the table. It is set toNOT NULL
, meaning it must always have a value, andAUTO_INCREMENT
, which automatically generates a unique value for each new row inserted into the table. -
first_name
: This column is of typeVARCHAR(30)
and stores the first name of a person. It is set toNOT NULL
, indicating that a value must be provided for this column. -
last_name
: This column is also of typeVARCHAR(30)
and stores the last name of a person. Similar to thefirst_name
column, it is set toNOT NULL
. -
email
: This column is of typeVARCHAR(70)
and is used to store the email address of a person. It is set toNOT NULL
to ensure a value is always present andUNIQUE
, meaning each email address must be unique within the table.
By executing this SQL statement, you will create a table named "persons" with the specified columns and their respective data types, constraints, and properties.
Please create an Issue for any improvements, suggestions or errors in the content.
You can also contact me using Linkedin for any other queries or feedback.