SQL to ER Diagram
HomeExamples › Hospital

Hospital Schema

A hospital management schema — patients and doctors, departments, appointments, inpatient admissions, and prescriptions — shown as an interactive ER diagram.

Open this schema in the editor → All examples

Interactive diagram

Drag to pan, scroll to zoom. Open it in the full editor to edit, rearrange and export.

About this schema

Doctors work in departments and see patients through appointments; patients may be admitted to a room during a stay, and doctors write prescriptions for medications against a patient visit.

Schema (SQL)

CREATE TABLE departments (
  id SERIAL PRIMARY KEY,
  name VARCHAR(120) NOT NULL
);
CREATE TABLE doctors (
  id SERIAL PRIMARY KEY,
  department_id INT REFERENCES departments(id),
  name VARCHAR(160), specialty VARCHAR(80)
);
CREATE TABLE patients (
  id SERIAL PRIMARY KEY,
  name VARCHAR(160) NOT NULL,
  date_of_birth DATE, phone VARCHAR(40)
);
CREATE TABLE appointments (
  id SERIAL PRIMARY KEY,
  patient_id INT NOT NULL REFERENCES patients(id),
  doctor_id INT NOT NULL REFERENCES doctors(id),
  scheduled_at TIMESTAMP, status VARCHAR(20)
);
CREATE TABLE rooms (
  id SERIAL PRIMARY KEY,
  number VARCHAR(10) UNIQUE, ward VARCHAR(40)
);
CREATE TABLE admissions (
  id SERIAL PRIMARY KEY,
  patient_id INT NOT NULL REFERENCES patients(id),
  room_id INT REFERENCES rooms(id),
  admitted_at TIMESTAMP, discharged_at TIMESTAMP
);
CREATE TABLE medications (
  id SERIAL PRIMARY KEY,
  name VARCHAR(160) NOT NULL, form VARCHAR(40)
);
CREATE TABLE prescriptions (
  id SERIAL PRIMARY KEY,
  appointment_id INT REFERENCES appointments(id),
  medication_id INT NOT NULL REFERENCES medications(id),
  dosage VARCHAR(60), duration_days INT
);

Open in the editor → to export it as PNG, SVG, Mermaid or DBML.

More examples