Advertisement
vallec

Untitled

Apr 3rd, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. use school_sport_clubs;
  2. /* 1 */
  3. DELIMITER $
  4. DROP PROCEDURE IF EXISTS testProc $
  5. CREATE PROCEDURE testProc()
  6. BEGIN
  7.  
  8. Select c.name, sg.location
  9. FROM coaches as c JOIN sportGroups as sg
  10. ON c.id = sg.coach_id;
  11. END $
  12. DELIMITER ;
  13.  
  14. CALL testProc();
  15.  
  16. /* 2 */
  17. DELIMITER $
  18. DROP PROCEDURE IF EXISTS testProc $
  19. CREATE PROCEDURE testProc(INOUT testParam VARCHAR(255))
  20. BEGIN
  21. SELECT testParam;
  22. SET testParam = 'Georgi';
  23. SELECT testParam;
  24. END$
  25. DELIMITER ;
  26.  
  27. set @test='Ivan';
  28. CALL testProc(@test);
  29. SELECT @test;
  30.  
  31. /* 3 */
  32.  
  33. use school_sport_clubs;
  34. #drop procedure checkMothTax;
  35. delimiter |
  36. CREATE procedure checkMothTax(IN studId INT, IN groupId INT, IN paymentMonth INT, IN paymentYear INT)
  37. BEGIN
  38. DECLARE result char(1);
  39. SET result = 0;
  40. IF( (SELECT paymentAmount
  41. FROM taxespayments
  42. WHERE student_id = studId
  43. AND group_id = groupId
  44. AND MONTH = paymentMonth
  45. AND year = paymentYear) IS NOT NULL)
  46. THEN
  47. SET result = 1;
  48. ELSE
  49. SET result = 0;
  50. END IF;
  51.  
  52. SELECT result as IsTaxPayed;
  53. end;
  54. |
  55. delimiter ;
  56.  
  57. CALL `school_sport_clubs`.`checkMothTax`(1, 1,1,2022);
  58.  
  59. /* 4 */
  60.  
  61. use school_sport_clubs;
  62.  
  63. drop procedure if exists getPaymentPeriod;
  64. delimiter |
  65. CREATE procedure getPaymentPeriod(IN studId INT, IN groupId INT, IN paymentYear INT)
  66. BEGIN
  67. DECLARE countOfMonths tinyint;
  68. DECLARE monthStr VARCHAR(10);
  69. DECLARE yearStr varchar(10);
  70. SET monthStr = 'MONTH';
  71. SET yearStr = 'YEAR';
  72.  
  73. SELECT COUNT(*)
  74. INTO countOfMonths
  75. FROM taxespayments
  76. WHERE student_id = studId
  77. AND group_id = groupId
  78. AND year = paymentYear;
  79.  
  80. CASE countOfMonths
  81. WHEN 0 THEN SELECT 'This student has not paid for this group/year!' as PAYMENT_PERIOD;
  82. WHEN 1 THEN SELECT concat('ONE_', monthStr) as PAYMENT_PERIOD;
  83. WHEN 3 THEN SELECT concat('THREE_',monthStr, 'S') as PAYMENT_PERIOD;
  84. WHEN 6 THEN SELECT concat('SIX_',monthStr,'S') as PAYMENT_PERIOD;
  85. WHEN 12 THEN SELECT yearStr as PAYMENT_PERIOD;
  86. ELSE
  87. SELECT concat(countOfMonths,monthStr,'S') as PAYMENT_PERIOD;
  88. END CASE;
  89. END;
  90. |
  91. DELIMITER ;
  92.  
  93. CALL getPaymentPeriod(1,1, 2022);
  94.  
  95. /* 5 */
  96. use school_sport_clubs;
  97.  
  98.  
  99. #drop procedure getAllPaymentsAmount;
  100. delimiter |
  101. CREATE procedure getAllPaymentsAmount(IN firstMonth INT, IN secMonth INT, IN paymentYear INT, IN studId INT)
  102. BEGIN
  103. DECLARE iterator int;
  104. IF(firstMonth >= secMonth)
  105. THEN
  106. SELECT 'Please enter correct months!' as RESULT;
  107. ELSE IF((SELECT COUNT(*)
  108. FROM taxesPayments
  109. WHERE student_id =studId ) = 0)
  110. THEN SELECT 'Please enter correct student_id!' as RESULT;
  111. ELSE
  112.  
  113. SET ITERATOR = firstMonth;
  114.  
  115. WHILE(iterator >= firstMonth AND iterator <= secMonth)
  116. DO
  117. SELECT student_id, group_id, paymentAmount, month
  118. FROM taxespayments
  119. WHERE student_id = studId
  120. AND year = paymentYear
  121. AND month = iterator;
  122.  
  123. SET iterator = iterator + 1;
  124. END WHILE;
  125. END IF;
  126.  
  127. END IF;
  128. END;
  129. |
  130. DELIMITER ;
  131.  
  132. CALL getAllPaymentsAmount(1,6,2022,1);
  133.  
  134. /* 6 */
  135.  
  136. use school_sport_clubs;
  137. #drop procedure getAllPaymentsAmountOptimized;
  138. delimiter |
  139. CREATE procedure getAllPaymentsAmountOptimized(IN firstMonth INT, IN secMonth INT, IN paymentYear INT, IN studId INT)
  140. BEGIN
  141. DECLARE iterator int;
  142. CREATE TEMPORARY TABLE tempTbl(
  143. student_id int,
  144. group_id int,
  145. paymentAmount double,
  146. month int
  147. ) ENGINE = Memory;
  148.  
  149.  
  150. IF(firstMonth >= secMonth)
  151. THEN
  152. SELECT 'Please enter correct months!' as RESULT;
  153. ELSE IF((SELECT COUNT(*)
  154. FROM taxesPayments
  155. WHERE student_id =studId ) = 0)
  156. THEN SELECT 'Please enter correct student_id!' as RESULT;
  157. ELSE
  158.  
  159. SET ITERATOR = firstMonth;
  160.  
  161. WHILE(iterator >= firstMonth AND iterator <= secMonth)
  162. DO
  163. INSERT INTO tempTbl
  164. SELECT student_id, group_id, paymentAmount, month
  165. FROM taxespayments
  166. WHERE student_id = studId
  167. AND year = paymentYear
  168. AND month = iterator;
  169.  
  170. SET iterator = iterator + 1;
  171. END WHILE;
  172. END IF;
  173.  
  174. END IF;
  175. SELECT *
  176. FROM tempTbl;
  177. DROP TABLE tempTbl;
  178. END;
  179. |
  180. DELIMITER ;
  181.  
  182. CALL getAllPaymentsAmountOptimized(1,6,2022,1);
  183.  
  184. /* zadachi */
  185.  
  186. /* 1 */
  187. use school_sport_clubs;
  188. DELIMITER $
  189. DROP PROCEDURE IF EXISTS GetCoachDetails;
  190. CREATE PROCEDURE GetCoachDetails(IN coach_name VARCHAR(255))
  191. BEGIN
  192. SELECT
  193. sg.location AS training_location,
  194. sg.dayOfWeek AS training_day,
  195. sg.hourOfTraining AS training_hour,
  196. s.name AS sport_name,
  197. st.name AS student_name,
  198. st.phone AS student_phone
  199. FROM
  200. coaches c
  201. JOIN
  202. sportGroups sg ON c.id = sg.coach_id
  203. JOIN
  204. sports s ON sg.sport_id = s.id
  205. JOIN
  206. student_sport ss ON sg.id = ss.sportGroup_id
  207. JOIN
  208. students st ON ss.student_id = st.id
  209. WHERE
  210. c.name = coach_name;
  211. END $
  212.  
  213. DELIMITER ;
  214.  
  215. CALL GetCoachDetails('Ivan Todorov Petkov');
  216.  
  217. /* 2 */
  218.  
  219. DELIMITER $
  220. DROP PROCEDURE IF EXISTS GetSportDetails;
  221. CREATE PROCEDURE GetSportDetails(IN sport_id INT)
  222. BEGIN
  223. SELECT
  224. s.name AS sport_name,
  225. st.name AS student_name,
  226. c.name AS coach_name
  227. FROM
  228. sports s
  229. JOIN
  230. sportGroups sg ON s.id = sg.sport_id
  231. JOIN
  232. coaches c ON sg.coach_id = c.id
  233. JOIN
  234. student_sport ss ON sg.id = ss.sportGroup_id
  235. JOIN
  236. students st ON ss.student_id = st.id
  237. WHERE
  238. s.id = sport_id;
  239. END $
  240.  
  241. DELIMITER ;
  242.  
  243. CALL GetSportDetails(1);
  244.  
  245. /* 3 */
  246. DELIMITER $
  247. DROP PROCEDURE IF EXISTS GetAveragePaymentsByStudent;
  248. CREATE PROCEDURE GetAveragePaymentsByStudent(IN student_name VARCHAR(255), IN payment_year YEAR)
  249. BEGIN
  250. SELECT
  251. AVG(tp.paymentAmount) AS average_payment
  252. FROM
  253. students as st
  254. JOIN
  255. taxesPayments as tp ON st.id = tp.student_id
  256. WHERE
  257. st.name = student_name
  258. AND YEAR(tp.dateOfPayment) = payment_year;
  259. END $
  260.  
  261. DELIMITER ;
  262.  
  263. CALL GetAveragePaymentsByStudent('Iliyan Ivanov', 2024);
  264.  
  265. /* 4 */
  266. DELIMITER $
  267.  
  268. CREATE PROCEDURE GetCoachGroupsCount(IN coach_name VARCHAR(255))
  269. BEGIN
  270. DECLARE groups_count INT;
  271.  
  272. SELECT COUNT(*) INTO groups_count
  273. FROM sportGroups AS sg
  274. JOIN coaches c ON sg.coach_id = c.id
  275. WHERE c.name = coach_name;
  276.  
  277. IF groups_count > 0 THEN
  278. SELECT groups_count AS coach_groups_count;
  279. ELSE
  280. SELECT 'Този треньор не води никакви групи.' AS message;
  281. END IF;
  282. END $
  283.  
  284. DELIMITER ;
  285.  
  286. CALL GetCoachGroupsCount('Ivan Todorov Petkov');
  287.  
  288. /* 5 */
  289. USE transaction_test;
  290.  
  291. DELIMITER $
  292. DROP PROCEDURE IF EXISTS transferMoney;
  293. CREATE PROCEDURE transferMoney(
  294. IN from_account_id INT,
  295. IN to_account_id INT,
  296. IN transfer_amount DOUBLE
  297. )
  298. BEGIN
  299. DECLARE from_balance DOUBLE;
  300.  
  301. SELECT amount INTO from_balance
  302. FROM customer_accounts
  303. WHERE id = from_account_id;
  304.  
  305. IF from_balance >= transfer_amount THEN
  306. UPDATE customer_accounts
  307. SET amount = amount - transfer_amount
  308. WHERE id = from_account_id;
  309.  
  310. UPDATE customer_accounts
  311. SET amount = amount + transfer_amount
  312. WHERE id = to_account_id;
  313.  
  314. SELECT 'Трансакцията е успешна.';
  315. ELSE
  316. SELECT 'Недостатъчно пари на сметката за трансакция.';
  317. END IF;
  318. END $
  319.  
  320. DELIMITER ;
  321.  
  322. call transferMoney(1, 2, 567.23);
  323.  
  324.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement