Advertisement
Guest User

assignment g5

a guest
Feb 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.84 KB | None | 0 0
  1. a)
  2. delimiter //
  3. CREATE FUNCTION `m_count`(p_name char(30)) RETURNS int(11)
  4. begin
  5. declare m_count integer;
  6. SELECT count(distinct movie_id) INTO m_count
  7. FROM imdb_movie.casting c, imdb_movie.person p,
  8. imdb_movie.movie m
  9. WHERE c.person_id = p.id
  10.  AND c.movie_id = m.id
  11.  AND m.kind_id=1
  12.  AND c.role_type_id=1
  13. AND p.name like p_name;
  14. return m_count;
  15. end;//
  16. delimiter ;
  17.  
  18. > SELECT m_count('Bacon, Kevin')
  19.  
  20. + ---------------------------- +
  21. | m_count('Bacon, Kevin')      |
  22. + ---------------------------- +
  23. | 90                           |
  24. + ---------------------------- +
  25. 1 rows
  26.  
  27.  
  28. b)
  29.  delimiter //
  30. create procedure movies(person_name char(20))
  31. begin
  32. SELECT distinct title
  33. FROM imdb_movie.casting c, imdb_movie.person p,
  34. imdb_movie.movie m
  35. WHERE c.person_id = p.id
  36. AND c.movie_id = m.id
  37. AND m.kind_id=1
  38. AND c.role_type_id=1
  39. AND p.name like person_name;
  40. end; //
  41. delimiter ;
  42.  
  43. > CALL movies('Mikkelsen, Mads')
  44.  
  45. + ---------- +
  46. | title      |
  47. + ---------- +
  48. | A Bond for Life: How James Bond Changed My Life |
  49. | Adams æbler |
  50. | Bleeder    |
  51. | Blinkende lygter |
  52. | Blomsterfangen |
  53. | Café Hector |
  54. | Casino Royale |
  55. | Clash of the Titans |
  56. | Coco Chanel & Igor Stravinsky |
  57. | De grønne slagtere |
  58. | Die Tür   |
  59. | Doctor Strange |
  60. | Dykkerdrengen |
  61. | Efter brylluppet |
  62. | Elsker dig for evigt |
  63. | En kongelig affære |
  64. | En kort en lang |
  65. | Exit       |
  66. | Flammen & Citronen |
  67. | I Am Dina  |
  68. | Jagten     |
  69. | King Arthur |
  70. | Michael Kohlhaas |
  71. | Monas verden |
  72. | Move On    |
  73. | Muumi ja punainen pyrstötähti |
  74. | Mænd & høns |
  75. | Nattens engel |
  76. | Nu         |
  77. | Prag       |
  78. | Pusher     |
  79. | Pusher II  |
  80. | Rogue One: A Star Wars Story |
  81. | The Necessary Death of Charlie Countryman |
  82. | The Salvation |
  83. | The Three Musketeers |
  84. | Tom Merritt |
  85. | Torremolinos 73 |
  86. | Valhalla Rising |
  87. | Vildspor   |
  88. | Wilbur Wants to Kill Himself |
  89. + ---------- +
  90. 41 rows
  91. c)
  92. delimiter //
  93. CREATE DEFINER=`spiotrm`@`%` PROCEDURE `titlec`(title_name char(20))
  94. BEGIN
  95. SELECT title from imdb_movie.movie m
  96. where m.title LIKE CONCAT('%',title_name,'%') order by production_year DESC limit 10 ;
  97. END; //
  98. delimiter ;
  99.  
  100. > CALL title('Titanic')
  101.  
  102. + ---------- +
  103. | title      |
  104. + ---------- +
  105. | Killer Zombies from the Titanic |
  106. | Titanic: The JonSong (Ft. Schmoyoho) |
  107. | Titanic: The Legend Goes On |
  108. | Titanic: The Legend Goes on - JonTron |
  109. | Resurrecting the Titanic |
  110. | Titanic in the Sky |
  111. | Titanic Boy |
  112. | Titanic    |
  113. | Titanic 2 World Premiere |
  114. | The Hidden Meaning in Titanic - Earthling Cinema |
  115. + ---------- +
  116. 10 rows
  117.  
  118. d)
  119. delimiter //
  120. CREATE FUNCTION `roles`(p_name char(30)) RETURNS varchar(100) CHARSET latin1
  121. begin
  122. declare m_count integer;
  123. declare a varchar(24);
  124. declare i int DEFAULT 0;
  125. declare done int DEFAULT 0;
  126. declare res_rows int DEFAULT 0;
  127. declare temp varchar(100) DEFAULT '';
  128. declare curl cursor for SELECT DISTINCT role
  129. FROM imdb_movie.casting
  130.  JOIN imdb_movie.person
  131.  ON person_id = person.id
  132.  JOIN imdb_movie.role_type
  133.  ON role_type_id = role_type.id
  134. WHERE name like p_name;
  135. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  136. open curl;
  137.     select FOUND_ROWS() into res_rows;
  138.         myloop:WHILE res_rows > i DO
  139.         fetch curl into a;
  140.         SET i = i+1;
  141.         IF i!=res_rows THEN
  142.             SET temp =CONCAT(temp,a,',');
  143.         ELSE
  144.             SET temp =CONCAT(temp,a);
  145.         END IF;
  146.         IF done = 1 THEN
  147.             LEAVE myloop;
  148.         END IF;
  149.     END WHILE;
  150.    
  151. close curl;
  152. return temp;
  153. end;//
  154. delimiter ;
  155.  
  156. > SELECT name,roles(name)
  157. FROM imdb_movie.person where name like 'De Niro, R%'
  158.  
  159. + --------- + ---------------- +
  160. | name      | roles(name)      |
  161. + --------- + ---------------- +
  162. | De Niro, Raphael | actor,producer   |
  163. | De Niro, Robert | actor,director,producer |
  164. | De Niro, Rocco | actor            |
  165. | de Niro, Ryan | cinematographer  |
  166. + --------- + ---------------- +
  167. 4 rows
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement