08- MySQL Create Table

0
251

In this tutorial called “MySQL Create Table”, we’re going to focus on how to create a table in the database. As we all know, Table is composed of columns and rows. Before we proceed, let’s put in mind that creating a table should require the following:

  • Name of the Table
  • Fields
  • and Definition for each field

Tips: A good programming habit should use the proper naming convention. For Example, if we’re going to create a table for people we will add a prefix ‘tbl‘ to make it ‘tblpeople’.

Generic SQL Syntax for creating Table:

CREATE TABLE table_name (column_name column_type);

 

Create a Table using MySQLi and PDO

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

 

We just created a table named ‘tblpeople‘ and it has four(4) fields, and these are ID, LNAME, FNAME, and ADDRESS.

 

Here are few Items that needs and Explanation:

  • Field Attribute NOT NULL – we use this because we do not want this field to be NULL. Meaning it will require the user to put some values to it as long as it will not be left as blank.
  • Field Attribute AUTO_INCREMENT – It means that MySQL will automatically add the value of the field every time a new record has been inserted.
  • Keyword PRIMARY KEY – this will be used to identify the field that it is unique in the table.

 

Please take note that on every table there should be a Primary Key, in our case ‘ID’.

MySQL Create Table

Here’s the example on how to create a table in PHP:

Example Using MySQLi (Object-Oriented):

 

Example Using MySQLi (Procedural):

 

Example Using PDO:

 

Our Previous Topic Called: 07- MySQL Create Database

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.