PHP | MySQL ( Creating Table )

What is a table?
In relational databases, and flat file databases, a table is a set of data elements using a model of vertical columns and horizontal rows, the cell being the unit where a row and column intersect. A table has a specified number of columns, but can have any number of rows.

Creating a MySQL Table Using MySQLi and PDO
We have already learned about creating databases in MySQL from PHP in this article. The steps to create table are similar to creating databases. The difference is instead of creating a new database we will connect to existing database and create a table in that database. To connect to an existing database we can pass an extra variable “database name” while connecting to MySQL.

The CREATE TABLE statement is used to create a table in MySQL.

In this article, a table named “employees”, with four columns: “id”, “firstname”, “lastname” and “email” will be created.

The data types that will be used are :

  1. VARCHAR:Holds a variable length string that can contain letters, numbers, and special characters. The maximum size is specified in parenthesis.
  2. INT :he INTEGER data type accepts numeric values with an implied scale of zero. It stores any integer value between -2147483648 to 2147483647.

The attributes that are used along with data types in this article are:

  1. NOT NULL: Each row must contain a value for that column, null values are not allowed.
  2. PRIMARY KEY: Used to uniquely identify the rows in a table. The column with PRIMARY KEY setting is often an ID number.

Creating tables in three different versions are described below: