Templates/Social & community

Q&A site database schema

Questions, answers with votes, accepted answers and tags.

6 tables, 8 relationships

Opens as a new diagram you can change. Free.

Tables

users

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

questions

  • iduuid not null
  • author_id> usersuuid not null
  • titletext not null
  • bodytext not null
  • accepted_answer_id> answersuuid
  • created_attimestamptz not null

answers

  • iduuid not null
  • question_id> questionsuuid not null
  • author_id> usersuuid not null
  • bodytext not null
  • created_attimestamptz not null

votes

  • iduuid not null
  • user_id> usersuuid not null
  • answer_id> answersuuid not null
  • valueinteger not null
  • created_attimestamptz not null

tags

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

question_tags

  • iduuid not null
  • question_id> questionsuuid not null
  • tag_id> tagsuuid 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 "answers" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "question_id" UUID NOT NULL,
  "author_id" UUID NOT NULL,
  "body" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "question_tags" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "question_id" UUID NOT NULL,
  "tag_id" UUID NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "questions" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "author_id" UUID NOT NULL,
  "title" TEXT NOT NULL,
  "body" TEXT NOT NULL,
  "accepted_answer_id" UUID,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

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

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

CREATE TABLE "votes" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "user_id" UUID NOT NULL,
  "answer_id" UUID NOT NULL,
  "value" INTEGER NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "questions" ADD CONSTRAINT "fk_questions_author_id" FOREIGN KEY ("author_id") REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "questions" ADD CONSTRAINT "fk_questions_accepted_answer_id" FOREIGN KEY ("accepted_answer_id") REFERENCES "answers" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

ALTER TABLE "answers" ADD CONSTRAINT "fk_answers_question_id" FOREIGN KEY ("question_id") REFERENCES "questions" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "answers" ADD CONSTRAINT "fk_answers_author_id" FOREIGN KEY ("author_id") REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "votes" ADD CONSTRAINT "fk_votes_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "votes" ADD CONSTRAINT "fk_votes_answer_id" FOREIGN KEY ("answer_id") REFERENCES "answers" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "question_tags" ADD CONSTRAINT "fk_question_tags_question_id" FOREIGN KEY ("question_id") REFERENCES "questions" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "question_tags" ADD CONSTRAINT "fk_question_tags_tag_id" FOREIGN KEY ("tag_id") REFERENCES "tags" ("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.