Skip to content

PHP Tutorial PHP and MySQL

Creating a Database and Table

Lesson 258 of 369 3 min read By Deepak Paswan Updated Oct 5, 2026

Before PHP can save anything, the database and its tables must exist. You can create them in phpMyAdmin (click around) or by running SQL.

Create the database

query.sql
SQL
CREATE DATABASE shop CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

utf8mb4 is the character set to use. It stores every character, including accents, Hindi letters and emoji. The older utf8 in MySQL is not full UTF-8, so avoid it.

Create a table

query.sql
SQL
USE shop;

CREATE TABLE users (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(190) NOT NULL UNIQUE,
    password_hash VARCHAR(255) NULL,
    role VARCHAR(20) NOT NULL DEFAULT 'user',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Read each column definition: name, type, then rules.

PartMeaning
INT UNSIGNEDA whole number from 0 up
AUTO_INCREMENTMySQL fills it with 1, 2, 3 … for each new row
PRIMARY KEYUnique identifier of the row
VARCHAR(100)Text up to 100 characters
NOT NULLThe column must always have a value
NULLThe column may be empty
UNIQUENo two rows may have the same value (two users cannot share an email)
DEFAULT 'user'Value used if you do not give one
TIMESTAMP ... CURRENT_TIMESTAMPFilled with the current time when the row is created
ENGINE=InnoDBThe storage engine that supports transactions and relations

Common data types

TypeUse for
INT, BIGINTWhole numbers
DECIMAL(10,2)Money. Exact decimals (not FLOAT, see Floating-Point Pitfalls)
VARCHAR(n)Short text (names, emails, titles)
TEXTLong text (article body)
TINYINT(1) or BOOLEANYes or no
DATE, DATETIME, TIMESTAMPDates and times
ENUM('a','b')One value from a short fixed list
JSONJSON documents

A table for products

query.sql
SQL
CREATE TABLE products (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(150) NOT NULL,
    price DECIMAL(10,2) NOT NULL,
    stock INT NOT NULL DEFAULT 0,
    category VARCHAR(50) NOT NULL DEFAULT 'general'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Relations: a foreign key

A post belongs to a user, so posts.user_id points to users.id.

query.sql
SQL
CREATE TABLE posts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    user_id INT UNSIGNED NOT NULL,
    title VARCHAR(200) NOT NULL,
    body TEXT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

The foreign key stops you from creating a post for a user that does not exist. ON DELETE CASCADE deletes a user’s posts when the user is deleted.

Add some data

query.sql
SQL
INSERT INTO users (name, email) VALUES
    ('Ana', 'ana@example.com'),
    ('Ben', 'ben@example.com');

Use phpMyAdmin

  1. Open phpMyAdmin (for example http://localhost/phpmyadmin).
  2. Click New, type a database name and choose utf8mb4_unicode_ci.
  3. Create a table, add columns, choose types, tick A_I (AUTO_INCREMENT) for the id and make it the primary key.
  4. Use the SQL tab to paste the commands above.

Design tips

  • Every table gets an id primary key.
  • Use singular or plural names consistently (users, products).
  • Store one fact in one place. Do not copy a user’s name into every post.
  • Index columns that you search often (email, category). Primary and unique keys are already indexed.
  • Never store plain passwords. The column holds a hash (see Password Hashing).
lightbulb
Keep the SQL in a file
Save your table definitions in a file such as schema.sql in your project, so you and your teammates can recreate the database at any time.