Skip to content

Database Schema

Kavindu Sahan Silva edited this page Oct 24, 2025 · 2 revisions

🗄️ Database Schema

Complete database schema documentation for HealthFirst Hospital Management System.

📋 Table of Contents

  1. Overview
  2. Entity Relationship Diagram
  3. Core Tables
  4. Table Definitions
  5. Relationships
  6. Indexes
  7. Sample Queries

🌟 Overview

Database: MySQL 8.0
Name: hospital_management
Character Set: utf8mb4
Collation: utf8mb4_unicode_ci
Engine: InnoDB

Statistics

  • Total Tables: 15
  • Total Views: 3
  • Stored Procedures: 5
  • Triggers: 8

🔗 Entity Relationship Diagram

┌─────────────┐       ┌──────────────┐       ┌─────────────┐
│    Users    │───────│   Patients   │───────│Appointments │
└─────────────┘       └──────────────┘       └─────────────┘
       │                     │                       │
       │                     │                       │
       │                     ▼                       ▼
       │              ┌──────────────┐       ┌─────────────┐
       │              │MedicalRecords│       │   Doctors   │
       │              └──────────────┘       └─────────────┘
       │                     │                      │
       ▼                     ▼                      │
┌─────────────┐       ┌──────────────┐              │
│   Roles     │       │Prescriptions │◄─────────────┘
└─────────────┘       └──────────────┘
                             │
                             ▼
                      ┌──────────────┐       ┌─────────────┐
                      │  Medicines   │───────│  Inventory  │
                      └──────────────┘       └─────────────┘
                             │
                             ▼
                      ┌──────────────┐
                      │   Orders     │
                      └──────────────┘
                             │
                             ▼
                      ┌──────────────┐       ┌─────────────┐
                      │   Invoices   │───────│  Payments   │
                      └──────────────┘       └─────────────┘

📊 Core Tables

1. users

Purpose: Stores all system users across different roles

CREATE TABLE users (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    phone VARCHAR(20),
    role ENUM('ADMIN', 'DOCTOR', 'PATIENT', 'PHARMACIST', 'ACCOUNTANT', 'RECEPTIONIST') NOT NULL,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    last_login TIMESTAMP NULL,
    INDEX idx_username (username),
    INDEX idx_email (email),
    INDEX idx_role (role),
    INDEX idx_active (is_active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Sample Data:

INSERT INTO users VALUES
(1, 'admin', 'admin', 'admin@hospital.com', 'System', 'Admin', '+1-555-0100', 'ADMIN', TRUE, NOW(), NOW(), NULL),
(2, 'doctor1', 'doctor1', 'sarah.wilson@hospital.com', 'Sarah', 'Wilson', '+1-555-0101', 'DOCTOR', TRUE, NOW(), NOW(), NULL),
(3, 'patient1', 'patient1', 'john.smith@email.com', 'John', 'Smith', '+1-555-0123', 'PATIENT', TRUE, NOW(), NOW(), NULL);

2. patients

Purpose: Stores patient-specific information

CREATE TABLE patients (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT NOT NULL,
    date_of_birth DATE NOT NULL,
    gender ENUM('MALE', 'FEMALE', 'OTHER') NOT NULL,
    blood_type VARCHAR(5),
    address TEXT,
    city VARCHAR(50),
    state VARCHAR(50),
    zip_code VARCHAR(10),
    emergency_contact_name VARCHAR(100),
    emergency_contact_phone VARCHAR(20),
    emergency_contact_relation VARCHAR(50),
    insurance_provider VARCHAR(100),
    insurance_policy_number VARCHAR(50),
    insurance_group_number VARCHAR(50),
    insurance_valid_until DATE,
    medical_history TEXT,
    allergies TEXT,
    chronic_conditions TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    UNIQUE KEY idx_user_id (user_id),
    INDEX idx_blood_type (blood_type),
    INDEX idx_gender (gender)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

3. doctors

Purpose: Stores doctor-specific information

CREATE TABLE doctors (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT NOT NULL,
    specialization VARCHAR(100) NOT NULL,
    license_number VARCHAR(50) UNIQUE NOT NULL,
    years_of_experience INT,
    qualifications TEXT,
    biography TEXT,
    consultation_fee DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
    languages_spoken VARCHAR(255),
    profile_image_url VARCHAR(255),
    is_available BOOLEAN DEFAULT TRUE,
    rating DECIMAL(3, 2) DEFAULT 0.00,
    total_reviews INT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    UNIQUE KEY idx_user_id (user_id),
    UNIQUE KEY idx_license (license_number),
    INDEX idx_specialization (specialization),
    INDEX idx_available (is_available)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

4. appointments

Purpose: Manages patient appointments with doctors

CREATE TABLE appointments (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    appointment_number VARCHAR(50) UNIQUE NOT NULL,
    patient_id BIGINT NOT NULL,
    doctor_id BIGINT NOT NULL,
    appointment_date DATE NOT NULL,
    appointment_time TIME NOT NULL,
    end_time TIME NOT NULL,
    appointment_type ENUM('FIRST_VISIT', 'FOLLOW_UP', 'CONSULTATION', 'EMERGENCY') NOT NULL,
    status ENUM('SCHEDULED', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED', 'NO_SHOW') DEFAULT 'SCHEDULED',
    reason TEXT,
    notes TEXT,
    cancellation_reason TEXT,
    cancelled_at TIMESTAMP NULL,
    cancelled_by BIGINT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    created_by BIGINT NOT NULL,
    FOREIGN KEY (patient_id) REFERENCES patients(id) ON DELETE CASCADE,
    FOREIGN KEY (doctor_id) REFERENCES doctors(id) ON DELETE CASCADE,
    FOREIGN KEY (cancelled_by) REFERENCES users(id) ON DELETE SET NULL,
    FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE,
    INDEX idx_appointment_number (appointment_number),
    INDEX idx_patient (patient_id),
    INDEX idx_doctor (doctor_id),
    INDEX idx_date (appointment_date),
    INDEX idx_status (status),
    INDEX idx_datetime (appointment_date, appointment_time),
    CONSTRAINT chk_times CHECK (end_time > appointment_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

5. medical_records

Purpose: Stores patient medical records and consultations

CREATE TABLE medical_records (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    record_number VARCHAR(50) UNIQUE NOT NULL,
    patient_id BIGINT NOT NULL,
    doctor_id BIGINT NOT NULL,
    appointment_id BIGINT NULL,
    visit_date DATE NOT NULL,
    chief_complaint TEXT,
    vital_signs JSON,
    physical_examination TEXT,
    diagnosis TEXT NOT NULL,
    icd10_code VARCHAR(20),
    secondary_diagnosis TEXT,
    treatment_plan TEXT,
    lab_tests_ordered TEXT,
    imaging_required TEXT,
    referrals TEXT,
    patient_education TEXT,
    follow_up_instructions TEXT,
    next_visit_date DATE NULL,
    is_draft BOOLEAN DEFAULT FALSE,
    addendum TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (patient_id) REFERENCES patients(id) ON DELETE CASCADE,
    FOREIGN KEY (doctor_id) REFERENCES doctors(id) ON DELETE CASCADE,
    FOREIGN KEY (appointment_id) REFERENCES appointments(id) ON DELETE SET NULL,
    INDEX idx_record_number (record_number),
    INDEX idx_patient (patient_id),
    INDEX idx_doctor (doctor_id),
    INDEX idx_visit_date (visit_date),
    INDEX idx_draft (is_draft)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Vital Signs JSON Structure:

{
  "height": 175,
  "weight": 80,
  "bmi": 26.1,
  "blood_pressure_systolic": 128,
  "blood_pressure_diastolic": 82,
  "heart_rate": 72,
  "temperature": 98.6,
  "respiratory_rate": 16,
  "oxygen_saturation": 98
}

6. prescriptions

Purpose: Manages prescriptions issued by doctors

CREATE TABLE prescriptions (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    prescription_number VARCHAR(50) UNIQUE NOT NULL,
    patient_id BIGINT NOT NULL,
    doctor_id BIGINT NOT NULL,
    medical_record_id BIGINT NULL,
    issue_date DATE NOT NULL,
    valid_until DATE NOT NULL,
    prescription_type ENUM('REGULAR', 'URGENT', 'CONTROLLED') DEFAULT 'REGULAR',
    status ENUM('ACTIVE', 'FULFILLED', 'EXPIRED', 'CANCELLED') DEFAULT 'ACTIVE',
    general_instructions TEXT,
    dietary_recommendations TEXT,
    precautions TEXT,
    follow_up_required BOOLEAN DEFAULT FALSE,
    follow_up_weeks INT NULL,
    is_draft BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (patient_id) REFERENCES patients(id) ON DELETE CASCADE,
    FOREIGN KEY (doctor_id) REFERENCES doctors(id) ON DELETE CASCADE,
    FOREIGN KEY (medical_record_id) REFERENCES medical_records(id) ON DELETE SET NULL,
    INDEX idx_prescription_number (prescription_number),
    INDEX idx_patient (patient_id),
    INDEX idx_doctor (doctor_id),
    INDEX idx_issue_date (issue_date),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

7. prescription_items

Purpose: Individual medications in prescriptions

CREATE TABLE prescription_items (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    prescription_id BIGINT NOT NULL,
    medicine_id BIGINT NOT NULL,
    dosage VARCHAR(100) NOT NULL,
    frequency VARCHAR(100) NOT NULL,
    route VARCHAR(50) NOT NULL,
    duration_days INT NOT NULL,
    quantity INT NOT NULL,
    refills_authorized INT DEFAULT 0,
    refills_used INT DEFAULT 0,
    special_instructions TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (prescription_id) REFERENCES prescriptions(id) ON DELETE CASCADE,
    FOREIGN KEY (medicine_id) REFERENCES medicines(id) ON DELETE CASCADE,
    INDEX idx_prescription (prescription_id),
    INDEX idx_medicine (medicine_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

8. medicines

Purpose: Pharmacy medicine inventory

CREATE TABLE medicines (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    sku VARCHAR(50) UNIQUE NOT NULL,
    medicine_name VARCHAR(200) NOT NULL,
    generic_name VARCHAR(200) NOT NULL,
    brand_name VARCHAR(200),
    manufacturer VARCHAR(200) NOT NULL,
    category VARCHAR(100) NOT NULL,
    form VARCHAR(50) NOT NULL,
    strength VARCHAR(50) NOT NULL,
    pack_size INT NOT NULL,
    description TEXT,
    active_ingredients TEXT,
    cost_price DECIMAL(10, 2) NOT NULL,
    selling_price DECIMAL(10, 2) NOT NULL,
    current_stock INT NOT NULL DEFAULT 0,
    minimum_stock_level INT NOT NULL DEFAULT 10,
    reorder_quantity INT NOT NULL DEFAULT 50,
    shelf_location VARCHAR(100),
    manufacture_date DATE NOT NULL,
    expiry_date DATE NOT NULL,
    storage_conditions TEXT,
    is_controlled BOOLEAN DEFAULT FALSE,
    is_prescription_required BOOLEAN DEFAULT TRUE,
    side_effects TEXT,
    contraindications TEXT,
    drug_interactions TEXT,
    dosage_instructions TEXT,
    image_url VARCHAR(255),
    barcode VARCHAR(100),
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_sku (sku),
    INDEX idx_medicine_name (medicine_name),
    INDEX idx_generic_name (generic_name),
    INDEX idx_category (category),
    INDEX idx_expiry (expiry_date),
    INDEX idx_stock (current_stock),
    INDEX idx_active (is_active),
    FULLTEXT idx_fulltext (medicine_name, generic_name, brand_name, description)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Clone this wiki locally