Skip to main content

ERD Template

Template untuk mendokumentasikan database schema menggunakan ERD (Entity Relationship Diagram) dengan Mermaid.

📊 Basic ERD Template


🎯 Complete E-Commerce ERD Example


📝 ERD Notation Guide

Relationship Cardinality

SymbolMeaningExample
||--o{One to ManyUser has many Orders
}o--||Many to OneOrder belongs to User
||--||One to OneOrder has one Payment
}o--o{Many to ManyProduct has many Tags
||--o|One to Zero or OneOrder may have Shipment

Field Constraints

ConstraintMeaning
PKPrimary Key
FKForeign Key
UKUnique Key
NOT NULLCannot be null
DEFAULT valueDefault value
>= 0Minimum value
Max NMaximum length

Data Types

TypeDescriptionExample
uuidUUID v4550e8400-e29b-41d4-a716-446655440000
stringVARCHAR"John Doe"
textTEXT/LONGTEXTLong description
intINTEGER42
decimalDECIMAL(10,2)99.99
booleanBOOLEANtrue/false
enumENUM"active", "inactive"
jsonJSON{"key": "value"}
timestampTIMESTAMP2024-01-01T10:00:00Z

🎨 Styling Tips

Color Coding (Optional)

Mermaid tidak support native color di ERD, tapi bisa di-customize via CSS:
/* Custom styling untuk Mintlify */
.entityBox {
  fill: #f9f9f9;
  stroke: #333;
}

.attributeBoxEven {
  fill: #ffffff;
}

.attributeBoxOdd {
  fill: #f5f5f5;
}

📚 Best Practices

  • Tables: UPPER_SNAKE_CASE atau PascalCase
  • Columns: snake_case
  • Foreign Keys: {table}_id (e.g., user_id)
  • Junction Tables: {table1}_{table2} (e.g., product_tag)
  • Selalu tambahkan constraint info (PK, FK, UK, NOT NULL)
  • Dokumentasikan enum values
  • Tambahkan default values
  • Jelaskan business rules di description
  • Gunakan nama relationship yang jelas
  • Dokumentasikan ON DELETE behavior
  • Jelaskan business logic di relationship
  • Update ERD setiap ada perubahan schema
  • Version control ERD bersama migration
  • Review ERD saat code review

🔄 Migration from ERD

Setelah ERD dibuat, generate migration SQL:
  • MySQL Migration
  • Down Migration
-- migrations/001_create_users_table.up.sql
CREATE TABLE users (
    id CHAR(36) PRIMARY KEY,
    email VARCHAR(255) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    name VARCHAR(255) NOT NULL,
    phone VARCHAR(20),
    role_id CHAR(36) NOT NULL,
    email_verified BOOLEAN DEFAULT FALSE,
    verification_token VARCHAR(255),
    verified_at TIMESTAMP NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP NULL,
    
    INDEX idx_users_email (email),
    INDEX idx_users_role_id (role_id),
    INDEX idx_users_deleted_at (deleted_at),
    
    FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

📊 Tools & Resources


Template Usage: Copy ERD template ini dan sesuaikan dengan schema database Anda. Pastikan semua relationship dan constraint terdokumentasi dengan jelas.
Pro Tip: Gunakan Atlas atau dbml untuk generate ERD otomatis dari database schema yang sudah ada!