PHP Create MySQL Tables
A data table has a unique name and consists of rows and columns.
Create MySQL tables using MySQLi and PDO
The CREATE TABLE statement is used to create a MySQL table.
Before creating the table, we need to use use myDB to select the database to operate on：
We will create a table named "MyGuests" with 5 columns: "id", "firstname", "lastname", "email" and "reg_date":
Notes in the table above:
Data type specifies what type of data the column can store. For complete data types, please refer to our Data Types Reference Manual .
After you set the data type, you can specify attributes for other options for each column:
- NOT NULL-Each row must contain a value (cannot be empty), null values are not allowed.
- DEFAULT value-set default value
- UNSIGNED-use unsigned numeric types, 0 and positive numbers
- AUTO INCREMENT-set the value of a MySQL field to automatically increase by 1 each time a new record is added
- PRIMARY KEY-Sets the unique ID of each record in the data table. Usually the column's PRIMARY KEY is set to the ID value, and AUTO_INCREMENT together.
Each table should have a primary key (this column is the "id" column). The primary key must contain a unique value.
The following example shows how to create a table in PHP: