Guest User

Untitled

a guest
Jul 15th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. CREATE TABLE users(
  2. id serial PRIMARY KEY,
  3. first_name text,
  4. last_name text,
  5. email_address text NOT NULL,
  6. screen_name text NOT NULL
  7. );
  8.  
  9. CREATE TABLE posts(
  10. id serial PRIMARY KEY,
  11. author_id int REFERENCES users ON DELETE RESTRICT,
  12. title text,
  13. content text,
  14. published timestamp DEFAULT now()
  15. );
  16.  
  17. CREATE TABLE tags (
  18. id serial PRIMARY KEY,
  19. tag text NOT NULL
  20. );
  21.  
  22. CREATE TABLE post_tags (
  23. post_id int REFERENCES posts(id) ON DELETE CASCADE,
  24. tag_id int REFERENCES tags(id) ON DELETE RESTRICT,
  25. PRIMARY KEY (post_id, tag_id)
  26. );
  27.  
  28. CREATE TABLE comments (
  29. id serial PRIMARY KEY,
  30. comment_text text,
  31. author_id integer REFERENCES users ON DELETE CASCADE NOT NULL,
  32. post_id integer REFERENCES posts ON DELETE CASCADE NOT NULL,
  33. referring_comment_id integer,
  34. FOREIGN KEY (referring_comment_id) REFERENCES comments(id) ON DELETE SET NULL
  35. );
  36.  
  37.  
  38. INSERT INTO users
  39. (email_address, screen_name) VALUES
  40. ('firstemail@email.com', 'JOdawg'),
  41. ('secondeamil@email.com', 'CDfox'),
  42. ('thirdemail@email.com', 'RCpanda');
  43.  
  44. INSERT INTO tags (tag) VALUES
  45. ('current events'), ('something important'), ('boring');
  46.  
  47. INSERT INTO posts (title, content, author_id) VALUES
  48. ('What do you want to know?', 'blah blah blah', 1),
  49. ('Random Title', 'Boring content', 1);
  50.  
  51. INSERT INTO post_tags (post_id, tag_id) VALUES
  52. (1, 2),
  53. (2, 3),
  54. (2, 1),
  55. (1, 1);
  56.  
  57. INSERT INTO comments (author_id, post_id) VALUES
  58. (2, 1),
  59. (1, 2);
  60.  
  61. INSERT INTO comments (author_id, post_id, referring_comment_id) VALUES
  62. (3, 1, 1),
  63. (2, 1, 2);
Add Comment
Please, Sign In to add comment