Templates/Health

Laboratory results database schema

Lab orders, collected samples, test panels and results with reference ranges.

7 tables, 7 relationships

Opens as a new diagram you can change. Free.

Tables

patients

  • iduuid not null
  • full_nametext not null
  • date_of_birthdate not null
  • created_attimestamptz not null

tests

  • iduuid not null
  • codetext not null
  • nametext not null
  • unittext
  • range_lownumeric
  • range_highnumeric
  • created_attimestamptz not null

panels

  • iduuid not null
  • nametext not null
  • created_attimestamptz not null

panel_tests

  • iduuid not null
  • panel_id> panelsuuid not null
  • test_id> testsuuid not null
  • created_attimestamptz not null

lab_orders

  • iduuid not null
  • patient_id> patientsuuid not null
  • panel_id> panelsuuid not null
  • ordered_bytext not null
  • prioritytext not null
  • created_attimestamptz not null

samples

  • iduuid not null
  • order_id> lab_ordersuuid not null
  • barcodetext not null
  • collected_attimestamptz not null
  • created_attimestamptz not null

results

  • iduuid not null
  • sample_id> samplesuuid not null
  • test_id> testsuuid not null
  • valuetext not null
  • flagtext
  • verified_attimestamptz
  • created_attimestamptz not null

As PostgreSQL

The same diagram exports to MySQL, MariaDB, SQLite, SQL Server and Oracle, and to Prisma, Drizzle, SQLAlchemy and Convex models.

CREATE TABLE "lab_orders" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "patient_id" UUID NOT NULL,
  "panel_id" UUID NOT NULL,
  "ordered_by" TEXT NOT NULL,
  "priority" TEXT DEFAULT 'routine' NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "panel_tests" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "panel_id" UUID NOT NULL,
  "test_id" UUID NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "panels" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "patients" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "full_name" TEXT NOT NULL,
  "date_of_birth" DATE NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "results" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "sample_id" UUID NOT NULL,
  "test_id" UUID NOT NULL,
  "value" TEXT NOT NULL,
  "flag" TEXT,
  "verified_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "samples" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "order_id" UUID NOT NULL,
  "barcode" TEXT NOT NULL UNIQUE,
  "collected_at" TIMESTAMPTZ NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "tests" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "code" TEXT NOT NULL UNIQUE,
  "name" TEXT NOT NULL,
  "unit" TEXT,
  "range_low" NUMERIC,
  "range_high" NUMERIC,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "panel_tests" ADD CONSTRAINT "fk_panel_tests_panel_id" FOREIGN KEY ("panel_id") REFERENCES "panels" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "panel_tests" ADD CONSTRAINT "fk_panel_tests_test_id" FOREIGN KEY ("test_id") REFERENCES "tests" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "lab_orders" ADD CONSTRAINT "fk_lab_orders_patient_id" FOREIGN KEY ("patient_id") REFERENCES "patients" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "lab_orders" ADD CONSTRAINT "fk_lab_orders_panel_id" FOREIGN KEY ("panel_id") REFERENCES "panels" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "samples" ADD CONSTRAINT "fk_samples_order_id" FOREIGN KEY ("order_id") REFERENCES "lab_orders" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "results" ADD CONSTRAINT "fk_results_sample_id" FOREIGN KEY ("sample_id") REFERENCES "samples" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "results" ADD CONSTRAINT "fk_results_test_id" FOREIGN KEY ("test_id") REFERENCES "tests" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

More Health templates

Start with one diagram, free

One diagram with two branches, no card needed. Upgrade when your team needs more.