This commit is contained in:
2025-10-23 08:49:33 +08:00
commit 45fb8dfeb8
6 changed files with 404 additions and 0 deletions

25
ddl/files.sql Normal file
View File

@@ -0,0 +1,25 @@
-- Files table: stores uploaded file metadata
CREATE TABLE IF NOT EXISTS files (
id BIGSERIAL PRIMARY KEY,
file_name VARCHAR(255) NOT NULL,
file_key VARCHAR(500) NOT NULL UNIQUE,
byte_size BIGINT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Index for looking up files by key (most common query)
CREATE INDEX IF NOT EXISTS idx_files_file_key ON files(file_key);
-- Index for sorting files by creation date
CREATE INDEX IF NOT EXISTS idx_files_created_at ON files(created_at DESC);
-- Index for file size queries
CREATE INDEX IF NOT EXISTS idx_files_byte_size ON files(byte_size);
-- Comments on columns
COMMENT ON TABLE files IS 'File metadata table for uploaded files';
COMMENT ON COLUMN files.id IS 'Primary key, auto-incrementing file ID';
COMMENT ON COLUMN files.file_name IS 'Original name of the uploaded file';
COMMENT ON COLUMN files.file_key IS 'Unique storage key/path for the file';
COMMENT ON COLUMN files.byte_size IS 'File size in bytes';
COMMENT ON COLUMN files.created_at IS 'Timestamp when the file was uploaded';

23
ddl/hashtags.sql Normal file
View File

@@ -0,0 +1,23 @@
-- Hashtags table: stores unique hashtag information
CREATE TABLE IF NOT EXISTS hashtags (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL UNIQUE,
usage_count BIGINT DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Index for looking up hashtags by name (most common query)
CREATE INDEX IF NOT EXISTS idx_hashtags_name ON hashtags(name);
-- Index for sorting hashtags by popularity
CREATE INDEX IF NOT EXISTS idx_hashtags_usage_count ON hashtags(usage_count DESC);
-- Index for trending hashtags (recent + popular)
CREATE INDEX IF NOT EXISTS idx_hashtags_created_usage ON hashtags(created_at DESC, usage_count DESC);
-- Comments on columns
COMMENT ON TABLE hashtags IS 'Hashtags table for categorizing posts';
COMMENT ON COLUMN hashtags.id IS 'Primary key, auto-incrementing hashtag ID';
COMMENT ON COLUMN hashtags.name IS 'Unique hashtag name';
COMMENT ON COLUMN hashtags.usage_count IS 'Number of times the hashtag has been used';
COMMENT ON COLUMN hashtags.created_at IS 'Timestamp when the hashtag was first created';

23
ddl/post_hashtags.sql Normal file
View File

@@ -0,0 +1,23 @@
-- Post_hashtags junction table: many-to-many relationship between posts and hashtags
CREATE TABLE IF NOT EXISTS post_hashtags (
post_id BIGINT NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
hashtag_id BIGINT NOT NULL REFERENCES hashtags(id) ON DELETE CASCADE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (post_id, hashtag_id)
);
-- Index for finding all posts with a specific hashtag
CREATE INDEX IF NOT EXISTS idx_post_hashtags_hashtag_id ON post_hashtags(hashtag_id, post_id);
-- Index for finding all hashtags for a specific post
CREATE INDEX IF NOT EXISTS idx_post_hashtags_post_id ON post_hashtags(post_id, hashtag_id);
-- Index for sorting by creation date
CREATE INDEX IF NOT EXISTS idx_post_hashtags_created_at ON post_hashtags(created_at DESC);
-- Comments on columns
COMMENT ON TABLE post_hashtags IS 'Junction table linking posts and hashtags';
COMMENT ON COLUMN post_hashtags.post_id IS 'Foreign key referencing posts table';
COMMENT ON COLUMN post_hashtags.hashtag_id IS 'Foreign key referencing hashtags table';
COMMENT ON COLUMN post_hashtags.created_at IS 'Timestamp when the hashtag was associated with the post';

30
ddl/posts.sql Normal file
View File

@@ -0,0 +1,30 @@
-- Posts table: stores blog post information
CREATE TABLE IF NOT EXISTS posts (
id BIGSERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
view_count BIGINT DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Index for sorting posts by creation date
CREATE INDEX IF NOT EXISTS idx_posts_created_at ON posts(created_at DESC);
-- Index for sorting posts by view count (popular posts)
CREATE INDEX IF NOT EXISTS idx_posts_view_count ON posts(view_count DESC);
-- Index for sorting posts by update date
CREATE INDEX IF NOT EXISTS idx_posts_updated_at ON posts(updated_at DESC);
-- Combined index for pagination queries
CREATE INDEX IF NOT EXISTS idx_posts_created_view ON posts(created_at DESC, view_count DESC);
-- Comments on columns
COMMENT ON TABLE posts IS 'Blog posts table containing article information';
COMMENT ON COLUMN posts.id IS 'Primary key, auto-incrementing post ID';
COMMENT ON COLUMN posts.title IS 'Post title';
COMMENT ON COLUMN posts.content IS 'Post content in text format';
COMMENT ON COLUMN posts.view_count IS 'Number of times the post has been viewed';
COMMENT ON COLUMN posts.created_at IS 'Timestamp when the post was created';
COMMENT ON COLUMN posts.updated_at IS 'Timestamp when the post was last updated';