alexarcan

dbd_accounts

Nov 13th, 2016
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. -- Insert 2 users
  2. insert into users values (1,'User1');
  3. insert into users values (2,'User2');
  4.  
  5. -- Creates 3 accounts for the first user
  6. insert into accounts values (1,1,2000);
  7. insert into accounts values (2,1,2000);
  8. insert into accounts values (3,1,2000);
  9.  
  10. -- Creates 2 acoounts for the second user
  11. insert into accounts values (4,2,3000);
  12. insert into accounts values (5,2,3000);
  13.  
  14. -- Some operations
  15. insert into operations values (1,1,1,1,500,null);
  16. insert into operations values (2,1,1,1,300,null);
  17. insert into operations values (3,1,1,3,300,4);
  18.  
  19. -- b
  20. set serveroutput on
  21. begin
  22. -- Deposit to account 1
  23. operation(1,1,1,500,null);
  24. -- Withdraw from account 1
  25. operation(1,1,2,500,null);
  26. -- Trying to deposit to a nonexistent account.
  27. operation(1,9,1,500,null);
  28. -- Transfer money
  29. operation(1,1,3,200,4);
  30. operation(1,2,1,200,null);
  31. end;
  32.  
  33. -- Selects the number of operations for each account
  34. select a.accountid, count(op.opid)
  35. from accounts a, operations op
  36. where a.accountid=op.accountid or a.accountid=op.toaid
  37. group by a.accountid;
  38.  
  39. -- Selects the users and the operations for each accounts, if there has been
  40. create view UsersOp (userid,accountid, noop) as
  41. select a.userid, a.accountid, count(op.opid) as noop
  42. from accounts a, operations op
  43. where a.accountid=op.accountid or a.accountid=op.toaid
  44. group by a.userid, a.accountid
  45. order by a.userid, noop desc;
  46.  
  47. create view AccOp (accountid, noop) as
  48. select a.accountid, count(op.opid) as noop
  49. from accounts a, operations op
  50. where a.accountid=op.accountid or a.accountid=op.toaid
  51. group by a.accountid
  52. order by noop desc;
  53.  
  54. select uo.userid, max(uo.noop)
  55. from usersop uo
  56. group by uo.userid;
  57.  
  58. select * from usersop
  59. where (userid,noop) in (select uo.userid, max(uo.noop)
  60. group by uo.userid;
  61.  
  62. -- c
  63. set serveroutput on
  64. begin
  65. listclients();
  66. end;
  67.  
  68. -- Selects the entire amount for all users
  69. create view usersamount (userid, total) as
  70. select u.userid, sum(a.sum) as total
  71. from users u, accounts a
  72. where u.userid=a.userid
  73. group by u.userid
  74. order by total desc;
  75.  
  76. -- d
  77. set serveroutput on
  78. begin
  79. -- Printing all users with a total greater than 200
  80. richest(200);
  81. richest(1000000);
  82. end;
  83.  
  84. -- b
  85. set serveroutput on
  86. begin
  87. operation(1,1,2,500000,null);
  88. end;
Add Comment
Please, Sign In to add comment