Getting Started

Table of Content

Table of Content

Table of Content

DDL to ER Diagram

Creating ER diagrams from foreign key references in DDL

Automatic Foreign Key Detection in Action

See how Datascale instantly transforms your SQL into visual ER diagrams

Drop your DDL into Datascale, and watch it automatically detect relationships and generate interactive ER diagrams. Here's how:

Input: Raw DDL

CREATE TABLE users (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL
);

CREATE TABLE wallets (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID UNIQUE NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    credits_balance INTEGER NOT NULL DEFAULT 0
)


Noted: Some DDL syntax might be not supported. If you don't see the diagram result, please simplify complex settings in your schema to include only column names and data types. Learn more SQL CREATE TABLE Statement:

CREATE TABEL table_name (
   column_name data_type
)


Output: Interactive ER Diagram

Datascale automatically:

  • Detects the foreign key: wallets.user_id → users.id

  • Maps the relationship: Visual connection between tables

  • Identifies constraints: UNIQUE, NOT NULL, CASCADE behaviors

  • Color-codes tables: Visual organization for better clarity

Key Benefits

🚀 Instant Visualization: No manual diagram creation 🔍 Relationship Discovery: Catches foreign keys you might miss 👥 Team Collaboration: Share visual schemas instantly 📋 Living Documentation: Diagrams stay synced with your code

What Gets Detected

From your DDL, Datascale automatically identifies:

  • Primary keys (id UUID PRIMARY KEY)

  • Foreign key references (REFERENCES users(id))

  • Cascade behaviors (ON DELETE CASCADE)

  • Column constraints (UNIQUE, NOT NULL)

  • Data types and defaults

Try It Yourself

  1. Paste your DDL statements

  2. Watch the automatic ER diagram generation

  3. Explore relationships visually

  4. Collaborate with your team

Example:

-- Users table (main user data)
CREATE TABLE users (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Wallets table (replaces private.wallet nested document)
CREATE TABLE wallets (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID UNIQUE NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    credits_balance INTEGER NOT NULL DEFAULT 0,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Topups table (replaces topups sub-collection)
CREATE TABLE topups (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    total_amount DECIMAL(10,2) NOT NULL,
    currency TEXT NOT NULL DEFAULT 'usd',
    amount_credits INTEGER NOT NULL,
    stripe_ref_id TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Usages table (replaces usages sub-collection)
CREATE TABLE usages (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    post_id UUID REFERENCES posts(id) ON DELETE SET NULL,
    amount_credits INTEGER NOT NULL, -- negative values for debits
    metadata JSONB, -- flexible metadata storage
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
)