Create MySQL database and tables in PHP

How to Create a MySQL Database and Tables?

You can create databases and tables easily in MySQL and with PHP too. Creating a Database Through MySQL you can create database very easily with below mentioned syntax: Syntax


CREATE DATABASE database_name
For execution of a MySQL query, you can use mysqli_query() build in function of PHP. You can create a database through PHP script. The example is mentioned below:

Code

<?php
  $connection = mysqli_connect("localhost", "marcus", "xyz123");

  if (!$connection)
  {
    die("Could not connect with MySQL: " . mysqli_connect_error());
  }

  if (mysqli_query($connection, "CREATE DATABASE my_database"))
  {
    echo "Database successfully created.";
  }
  else
  {
    echo "Error occurred: " . mysqli_error($connection);
  }

  mysqli_close($connection);
?>
Creating a Table

Through MySQL you can also create tables very easily with below mentioned syntax:

Syntax

CREATE TABLE your_table_name
(
  column_name1,
  column_name2,
  column_name3
);
Before creating a table you should select a database, for create a table. You can select a database with using mysqli_select_db() built in function. You should put create table statement or query in mysqli_query function for creating a table. PHP allows you to programmatically generate new tables in a MySQL database using SQL queries. In below mentioned example we created a table with name "Student", which have three columns. The column names would be "FirstName", "LastName" and "EnrollmentNo":

Code

<?php
  $connection = mysqli_connect("localhost", "marcus", "xyz123");

  if (!$connection)
  {
    die("Could not connect with MySQL: " . mysqli_connect_error());
  }

  // Create database
  if (mysqli_query($connection, "CREATE DATABASE my_database"))
  {
    echo "Database successfully created";
  }
  else
  {
    echo "Error occurred: " . mysqli_error($connection);
  }

  // Select database
  mysqli_select_db($connection, "my_database");

  $sql_query = "CREATE TABLE Student
  (
    FirstName varchar(15),
    LastName varchar(15),
    EnrollmentNo int(11)
  )";

  // Execute query
  if (mysqli_query($connection, $sql_query))
  {
    echo "Student table successfully created.";
  }
  else
  {
    echo "Error occurred: " . mysqli_error($connection);
  }

  // Close connection
  mysqli_close($connection);
?>
Primary Keys and Auto Increment Fields

It is called that for good database design or database management you must a primary key in tables. A primary key plays a very important role, it is used to uniquely identify rows in a table. Each primary key's value remain unique within the table. A primary key's value cannot be null because the database engine requires a value to locate the record. We have an example mentioned below, which sets the employeeID field as the primary key. The primary key usually a numerical value, and is usually used with the AUTO_INCREMENT setting in MySQL. In MySQL you can create a field as AUTO_INCREMENT, AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is inserted.

Code

<?php
  $sql_query = "CREATE TABLE Employee
  _details
  (
    employeeID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(employeeID),
    FirstName varchar(15),
    LastName varchar(15),
    phoneNo varchar(15)
  )";
  mysqli_query($sql_query,$connection);
?>

FAQ 1. What is a primary key in MySQL? A primary key is a column or combination of columns that uniquely identifies each record in a MySQL table. A primary key cannot contain duplicate or NULL values. 2. What is AUTO_INCREMENT in MySQL? AUTO_INCREMENT is a MySQL attribute that automatically generates a sequential numeric value whenever a new record is inserted into a table. It is commonly used with primary key columns.