Skip to content

Latest commit

 

History

History
62 lines (45 loc) · 3.6 KB

12d.md

File metadata and controls

62 lines (45 loc) · 3.6 KB

Stars Badge Forks Badge Pull Requests Badge Issues Badge GitHub contributors Visitors

Don't forget to hit the ⭐ if you like this repo.

PHP MySQL Create Tables

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:

  1. id: This column is of type INT and serves as the primary key for the table. It is set to NOT NULL, meaning it must always have a value, and AUTO_INCREMENT, which automatically generates a unique value for each new row inserted into the table.

  2. first_name: This column is of type VARCHAR(30) and stores the first name of a person. It is set to NOT NULL, indicating that a value must be provided for this column.

  3. last_name: This column is also of type VARCHAR(30) and stores the last name of a person. Similar to the first_name column, it is set to NOT NULL.

  4. email: This column is of type VARCHAR(70) and is used to store the email address of a person. It is set to NOT NULL to ensure a value is always present and UNIQUE, 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.

Contribution 🛠️

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.

Visitors