PHP Create a DB table

Creating a database is not enough to use it. To be able to insert any data we must first create tables, with colums and rows to organise our data.

We will start creating a very simple table called users. Our table will have three columns: id (the primary key), name and surname.

We can create our table with a GUI like phpMyAdmin, or run a SQL command manually:

When you create a table you have to list its columns, and for each of them you should provide:

  • column name: (mandatory)
  • datatype (mandatory) e.g. INT, VARCHAR, FLOAT, TEXT etc
  • length in parenthesis
  • attributes e.g. UNSIGNED
  • NULL / NOT NULL
  • extra e.g. AUTO-INCREMENT
  • set if it’s a primary/foreign key