Templates/Social & community

Gamification database schema

Points ledger, levels, badges and streaks for an engaged community.

6 tables, 4 relationships

Opens as a new diagram you can change. Free.

Tables

players

  • iduuid not null
  • usernametext not null
  • created_attimestamptz not null

levels

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

point_events

  • iduuid not null
  • player_id> playersuuid not null
  • pointsinteger not null
  • reasontext not null
  • created_attimestamptz not null

badges

  • iduuid not null
  • keytext not null
  • nametext not null
  • descriptiontext not null
  • created_attimestamptz not null

player_badges

  • iduuid not null
  • player_id> playersuuid not null
  • badge_id> badgesuuid not null
  • awarded_attimestamptz not null
  • created_attimestamptz not null

streaks

  • iduuid not null
  • player_id> playersuuid not null
  • currentinteger not null
  • longestinteger not null
  • last_active_ondate not null
  • 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 "badges" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "key" TEXT NOT NULL UNIQUE,
  "name" TEXT NOT NULL,
  "description" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

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

CREATE TABLE "player_badges" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "player_id" UUID NOT NULL,
  "badge_id" UUID NOT NULL,
  "awarded_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "players" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "username" TEXT NOT NULL UNIQUE,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "point_events" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "player_id" UUID NOT NULL,
  "points" INTEGER NOT NULL,
  "reason" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "streaks" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "player_id" UUID NOT NULL UNIQUE,
  "current" INTEGER DEFAULT 0 NOT NULL,
  "longest" INTEGER DEFAULT 0 NOT NULL,
  "last_active_on" DATE NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "point_events" ADD CONSTRAINT "fk_point_events_player_id" FOREIGN KEY ("player_id") REFERENCES "players" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "player_badges" ADD CONSTRAINT "fk_player_badges_player_id" FOREIGN KEY ("player_id") REFERENCES "players" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "player_badges" ADD CONSTRAINT "fk_player_badges_badge_id" FOREIGN KEY ("badge_id") REFERENCES "badges" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "streaks" ADD CONSTRAINT "fk_streaks_player_id" FOREIGN KEY ("player_id") REFERENCES "players" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

More Social & community templates

Start with one diagram, free

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