veselinsavov

dbcourse_sql_code

Apr 4th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.38 KB | None | 0 0
  1. /* create schema */
  2. DROP DATABASE IF EXISTS dbcourse;
  3. CREATE SCHEMA `dbcourse` ;
  4.  
  5. USE dbcourse;
  6.  
  7. CREATE TABLE posts(
  8.     id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  9.     title VARCHAR(200) NOT NULL
  10. );
  11.  
  12. CREATE TABLE comments(
  13.     id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  14.     post_id INT UNSIGNED NOT NULL,
  15.     content VARCHAR(500),
  16.     FOREIGN KEY(post_id) REFERENCES posts(id)
  17. );
  18.  
  19. CREATE TABLE emotion_type(
  20.     id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  21.     name VARCHAR(500) NOT NULL
  22. );
  23.  
  24. CREATE TABLE emotions(
  25.     id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  26.     post_id INT UNSIGNED NOT NULL,
  27.     emotion_id INT UNSIGNED NOT NULL,
  28.     FOREIGN KEY(post_id) REFERENCES posts(id),
  29.     FOREIGN KEY(emotion_id) REFERENCES emotion_type(id)
  30. );
  31.  
  32. /* insert data */
  33. INSERT INTO posts
  34.     (title)
  35. VALUES
  36.     ("First post"),
  37.     ("Second post"),
  38.     ("Tirth post");
  39.    
  40.    
  41. INSERT INTO comments
  42.     (post_id, content)
  43. VALUES
  44.     (1, "First comment"),
  45.     (1, "Second comment"),
  46.     (1, "Tirth comment"),
  47.     (2, "Gore dolu"),
  48.     (2, "Ala bala"),
  49.     (3, "Bla bla");
  50.    
  51.    
  52. INSERT INTO emotion_type
  53.     (name)
  54. VALUES
  55.     ("Like"),
  56.     ("Love"),
  57.     ("Sad"),
  58.     ("Wow"),
  59.     ("Angry"),
  60.     ("Haha");
  61.    
  62. INSERT INTO emotions
  63.     (post_id, emotion_id)
  64. VALUES
  65.     (1, 1),
  66.     (1, 1),
  67.     (1, 3),
  68.     (1, 2),
  69.     (2, 4),
  70.     (2, 3),
  71.     (3, 2),
  72.     (3, 1);
  73.  
  74. /* ---------------------------- */
  75. /* Task 8*/
  76. USE dbcourse;
  77. SELECT p.title
  78. FROM posts p
  79. WHERE p.title like "%F%"
  80.  
  81. /* ---------------------------- */
  82. /* Task 9*/
  83. USE dbcourse;
  84. SELECT et.name, count(et.id) as 'Emotion_total_count'
  85. FROM emotions e
  86. INNER JOIN emotion_type et
  87. ON e.emotion_id = et.id
  88. GROUP BY e.emotion_id
  89.  
  90. /* ---------------------------- */
  91. /* Task 10*/
  92. USE dbcourse;
  93. SELECT e.post_id, et.name
  94. FROM emotions e
  95. LEFT JOIN emotion_type et
  96. ON e.emotion_id = et.id
  97. UNION
  98. SELECT e.post_id, et.name
  99. FROM emotions e
  100. LEFT JOIN emotion_type et
  101. ON e.emotion_id = et.id
  102.  
  103. /* ---------------------------- */
  104. /* Task 11*/
  105. USE dbcourse;
  106. SELECT p.title, count(c.id)
  107. FROM posts p
  108. JOIN comments c
  109. ON p.id = c.post_id
  110. GROUP BY p.id
  111.  
  112. /* ---------------------------- */
  113. /* Task 12*/
  114. USE dbcourse;
  115. DELIMITER //
  116. CREATE PROCEDURE GetPostComments(post_id INT)
  117.  BEGIN
  118.      SELECT c.content
  119.      FROM comments c
  120.      WHERE c.post_id = post_id;
  121.  END //
  122. DELIMITER ;
  123.  
  124. /* demo call */
  125. USE dbcourse;
  126. CALL GetPostComments(1);
Advertisement
Add Comment
Please, Sign In to add comment