Skip to content

Instantly share code, notes, and snippets.

@sergii
Forked from simonw/posts_comments_votes.sql
Created January 5, 2023 16:10
Show Gist options
  • Save sergii/948fc5eaca0f1e423f079d56a1cd56c9 to your computer and use it in GitHub Desktop.
Save sergii/948fc5eaca0f1e423f079d56a1cd56c9 to your computer and use it in GitHub Desktop.
CREATE TABLE posts (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
content TEXT NOT NULL
);
CREATE TABLE comments (
id INTEGER PRIMARY KEY,
content TEXT NOT NULL,
post_id INTEGER NOT NULL,
FOREIGN KEY (post_id) REFERENCES posts(id)
);
CREATE TABLE votes (
id INTEGER PRIMARY KEY,
comment_id INTEGER NOT NULL,
FOREIGN KEY (comment_id) REFERENCES comments(id)
);
INSERT INTO posts (id, title, content) VALUES
(1, 'First post', 'This is the first post'),
(2, 'Second post', 'This is the second post'),
(3, 'Third post', 'This is the third post');
INSERT INTO comments (id, content, post_id) VALUES
(1, 'First comment on first post', 1),
(2, 'Second comment on first post', 1),
(3, 'First comment on second post', 2),
(4, 'Second comment on second post', 2),
(5, 'First comment on third post', 3),
(6, 'Second comment on third post', 3);
INSERT INTO votes (id, comment_id) VALUES
(1, 1), (2, 1), (3, 1),
(4, 2), (5, 2),
(6, 3), (7, 3), (8, 3), (9, 3),
(10, 4), (11, 4), (12, 4), (13, 4), (14, 4),
(15, 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment