📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials MySQL Creating Databases and Tables

Creating Databases and Tables

5 min read Quiz at the end
Create databases with utf8mb4 charset and tables with proper types, indexes, and auto-updated timestamps.

Creating Databases and Tables

CREATE DATABASE IF NOT EXISTS mydb
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci;

USE mydb;

CREATE TABLE users (
    id          INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name        VARCHAR(100) NOT NULL,
    email       VARCHAR(255) NOT NULL UNIQUE,
    age         TINYINT UNSIGNED,
    bio         TEXT,
    avatar_url  VARCHAR(500),
    is_active   TINYINT(1) NOT NULL DEFAULT 1,
    created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_email (email),
    INDEX idx_active (is_active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS old_table;
DROP DATABASE IF EXISTS testdb;
Topic Quiz · 1 questions

Test your understanding before moving on

1. Which option prevents errors when creating a table that already exists?
💡 CREATE TABLE IF NOT EXISTS skips creation silently if the table already exists.