Advertisement
BugByte

Untitled

May 21st, 2021 (edited)
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.23 KB | None | 0 0
  1. use lab1;
  2.  
  3. create table Votes (cityname varchar(50) PRIMARY KEY, votecount int);
  4. insert into Votes (cityname , votecount) values ('Mars City One',34);
  5. insert into Votes (cityname , votecount) values ('New Gothenburg',11);
  6. insert into Votes (cityname , votecount) values ('Picardia',1);
  7. update Votes set votecount = votecount+3 where cityname='New Gothenburg';
  8.  
  9. select * from Votes;
  10.  
  11. set @remaining_votes = 1337;
  12. select @remaining_votes;
  13.  
  14. drop procedure if exists updateVotes;
  15.  
  16. delimiter //
  17. create procedure updateVotes()
  18. begin
  19.     if not exists (select * from Votes where cityname = 'cityname') then
  20.         insert into Votes (cityname,votecount) select 'cityname',1337 - sum(votecount) from Votes;
  21.     else
  22.         update Votes set votecount = @remaining_votes where cityname = 'cityname';
  23.     end if;
  24. end//
  25. delimiter ;
  26.  
  27. call updateVotes;
  28.  
  29. select * from Votes;
  30.  
  31. drop trigger if exists setRemainingVotes;
  32.  
  33. delimiter //
  34. create trigger setRemainingVotes after insert on Votes
  35. for each row
  36.     begin
  37.         set @remaining_votes = 1337 - (select sum(votecount) from Votes where cityname <> 'cityname');
  38.     end//
  39. delimiter ;
  40.  
  41. insert into votes values ('Ricardo',10);
  42. insert into votes values ('Picardo',21);
  43. call updateVotes;
  44.  
  45. select * from Votes;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement