0% found this document useful (0 votes)
91 views5 pages

Database Schema

This document outlines the MySQL database schema for a Reddit-like web app MVP, detailing the tables, columns, data types, relationships, and indexes necessary for managing users, posts, comments, subreddits, and votes. It specifies the core features included in the schema while excluding advanced features and scalability beyond 1,000 users. The document also includes sample queries and an indexing strategy to ensure efficient data retrieval and integrity.

Uploaded by

tinkesh509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views5 pages

Database Schema

This document outlines the MySQL database schema for a Reddit-like web app MVP, detailing the tables, columns, data types, relationships, and indexes necessary for managing users, posts, comments, subreddits, and votes. It specifies the core features included in the schema while excluding advanced features and scalability beyond 1,000 users. The document also includes sample queries and an indexing strategy to ensure efficient data retrieval and integrity.

Uploaded by

tinkesh509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Database Schema: Reddit-Like Web App

Document Title: Database Schema

Version: 1.0

Related Documents: Project Overview (v1.0), Feature Requirements (v1.0), Technical Design
Document (v2.0), UI/UX Design (v1.0)

Purpose
This document defines the MySQL database schema for a Reddit-like web app MVP. It specifies
tables, columns, data types, relationships, and indexes to store and manage users, posts,
comments, subreddits, and votes, ensuring efficient data retrieval and integrity for the app’s features.

Scope
●​ In Scope: Database design for core features (user accounts, posts, comments, voting,
subreddits, home feed, search).
●​ Out of Scope: Schema for advanced features (e.g., moderation logs, analytics) or scalability
beyond 1,000 users.

Database Overview
●​ Type: Relational (MySQL)
●​ Hosting: AWS RDS (MySQL instance)
●​ Engine: InnoDB (for transactional support and foreign key constraints)
●​ Character Set: UTF-8 (for multilingual content)

Tables and Schema


Below are the tables, their columns, data types, constraints, and relationships.
1. Users
●​ Purpose: Store user account information.
●​ Columns:
○​ id (BIGINT, PRIMARY KEY, AUTO_INCREMENT)
■​ Unique identifier for each user.
○​ username (VARCHAR(20), NOT NULL, UNIQUE)
■​ User’s display name (max 20 characters).
○​ email (VARCHAR(255), NOT NULL, UNIQUE)
■​ User’s email for login and notifications.
○​ password_hash (VARCHAR(255), NOT NULL)
■​ Hashed password (e.g., bcrypt).
○​ created_at (DATETIME, NOT NULL, DEFAULT CURRENT_TIMESTAMP)
■​ Account creation timestamp.
●​ Indexes:
○​ Primary: id
○​ Unique: username, email

2. Subreddits
●​ Purpose: Store community groups (like Reddit’s subreddits).
●​ Columns:
○​ id (BIGINT, PRIMARY KEY, AUTO_INCREMENT)
■​ Unique identifier for each subreddit.
○​ name (VARCHAR(20), NOT NULL, UNIQUE)
■​ Subreddit name (e.g., “Technology”, max 20 characters).
○​ description (VARCHAR(200), NOT NULL)
■​ Brief description (max 200 characters).
○​ creator_id (BIGINT, NOT NULL, FOREIGN KEY → Users(id))
■​ ID of the user who created the subreddit.
○​ created_at (DATETIME, NOT NULL, DEFAULT CURRENT_TIMESTAMP)
■​ Subreddit creation timestamp.
●​ Indexes:
○​ Primary: id
○​ Unique: name
○​ Foreign Key: creator_id

3. Posts
●​ Purpose: Store user-generated posts.
●​ Columns:
○​ id (BIGINT, PRIMARY KEY, AUTO_INCREMENT)
■​ Unique identifier for each post.
○​ user_id (BIGINT, NOT NULL, FOREIGN KEY → Users(id))
■​ ID of the posting user.
○​ subreddit_id (BIGINT, NOT NULL, FOREIGN KEY → Subreddits(id))
■​ ID of the subreddit where the post belongs.
○​ title (VARCHAR(200), NOT NULL)
■​ Post title (max 200 characters).
○​ content (TEXT, NULL)
■​ Post content (text or URL, max 10,000 characters).
○​ upvotes (INT, NOT NULL, DEFAULT 0)
■​ Number of upvotes.
○​ downvotes (INT, NOT NULL, DEFAULT 0)
■​ Number of downvotes.
○​ created_at (DATETIME, NOT NULL, DEFAULT CURRENT_TIMESTAMP)
■​ Post creation timestamp.
○​ updated_at (DATETIME, NULL)
■​ Last edit timestamp (nullable).
●​ Indexes:
○​ Primary: id
○​ Foreign Keys: user_id, subreddit_id
○​ Index: created_at (for sorting feeds)

4. Comments
●​ Purpose: Store comments and replies on posts.
●​ Columns:
○​ id (BIGINT, PRIMARY KEY, AUTO_INCREMENT)
■​ Unique identifier for each comment.
○​ post_id (BIGINT, NOT NULL, FOREIGN KEY → Posts(id))
■​ ID of the post being commented on.
○​ user_id (BIGINT, NOT NULL, FOREIGN KEY → Users(id))
■​ ID of the commenting user.
○​ parent_comment_id (BIGINT, NULL, FOREIGN KEY → Comments(id))
■​ ID of the parent comment (NULL for top-level, 1-level nesting for MVP).
○​ content (TEXT, NOT NULL)
■​ Comment text (max 5,000 characters).
○​ upvotes (INT, NOT NULL, DEFAULT 0)
■​ Number of upvotes.
○​ downvotes (INT, NOT NULL, DEFAULT 0)
■​ Number of downvotes.
○​ created_at (DATETIME, NOT NULL, DEFAULT CURRENT_TIMESTAMP)
■​ Comment creation timestamp.
○​ updated_at (DATETIME, NULL)
■​ Last edit timestamp (nullable).
●​ Indexes:
○​ Primary: id
○​ Foreign Keys: post_id, user_id, parent_comment_id
○​ Index: created_at

5. Votes
●​ Purpose: Track individual votes on posts and comments to enforce one-vote-per-user and
calculate totals.
●​ Columns:
○​ id (BIGINT, PRIMARY KEY, AUTO_INCREMENT)
■​ Unique identifier for each vote.
○​ user_id (BIGINT, NOT NULL, FOREIGN KEY → Users(id))
■​ ID of the voting user.
○​ target_id (BIGINT, NOT NULL)
■​ ID of the post or comment being voted on.
○​ target_type (ENUM(‘post’, ‘comment’), NOT NULL)
■​ Specifies if the vote is for a post or comment.
○​ vote (TINYINT, NOT NULL, CHECK (vote IN (1, -1)))
■​ 1 for upvote, -1 for downvote.
●​ Indexes:
○​ Primary: id
○​ Foreign Key: user_id
○​ Composite Index: user_id, target_id (to enforce one vote per user per target)
○​ Index: target_id

Relationships
●​ Users → Posts: One-to-Many (one user can create many posts).
●​ Users → Comments: One-to-Many (one user can write many comments).
●​ Users → Subreddits: One-to-Many (one user can create many subreddits).
●​ Subreddits → Posts: One-to-Many (one subreddit contains many posts).
●​ Posts → Comments: One-to-Many (one post has many comments).
●​ Comments → Comments: Self-referential, One-to-Many (one comment can have many
replies, 1-level nesting).
●​ Users → Votes: One-to-Many (one user can cast many votes).
●​ Votes → Posts/Comments: Many-to-One (many votes apply to a single post or comment
via target_id and target_type).

Sample Queries
1.​ Home Feed:
○​ SELECT p.*, [Link], [Link] FROM Posts p JOIN Users u ON
p.user_id = [Link] JOIN Subreddits s ON p.subreddit_id = [Link] ORDER
BY [Link] - [Link] DESC LIMIT 50;
2.​ Post Comments:
○​ SELECT c.*, [Link] FROM Comments c JOIN Users u ON c.user_id =
[Link] WHERE c.post_id = ? ORDER BY c.created_at ASC;
3.​ User Votes on a Post:
○​ SELECT vote FROM Votes WHERE user_id = ? AND target_id = ? AND
target_type = 'post';
4.​ Search Posts:
○​ SELECT * FROM Posts WHERE title LIKE ? OR content LIKE ? LIMIT 20;

Indexing Strategy
●​ Primary Keys: id on all tables for unique identification.
●​ Foreign Keys: Enforce referential integrity (e.g., user_id, post_id).
●​ Performance Indexes:
○​ created_at on Posts and Comments for sorting feeds.
○​ username and email on Users for login lookups.
○​ name on Subreddits for quick subreddit access.
○​ Composite user_id, target_id on Votes for vote uniqueness checks.

Assumptions
●​ Initial data volume: <1,000 users, <10,000 posts/comments.
●​ MySQL free tier on AWS RDS (e.g., 20GB storage) suffices for MVP.
●​ No full-text search indexing (e.g., MySQL FULLTEXT) in MVP; basic LIKE queries used.

You might also like