Guest User

Untitled

a guest
Sep 6th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. drop table if exists payments;
  2. drop table if exists payment_types;
  3. drop table if exists customers;
  4.  
  5. create table payment_types
  6. (
  7. pay_type_id tinyint unsigned not null auto_increment primary key,
  8. name varchar(255) unique not null
  9. )
  10. engine=innodb;
  11.  
  12. create table customers
  13. (
  14. cust_id int unsigned not null auto_increment primary key,
  15. email varchar(512) unique not null,
  16. total_amount_paid decimal(10,2) not null default 0
  17. )
  18. engine=innodb;
  19.  
  20. create table payments
  21. (
  22. pay_id int unsigned not null auto_increment primary key,
  23. cust_id int unsigned not null,
  24. pay_type_id tinyint unsigned not null,
  25. pay_date datetime not null,
  26. amount decimal(10,2) not null default 0,
  27. key (pay_date),
  28. foreign key (cust_id) references customers(cust_id),
  29. foreign key (pay_type_id) references payment_types(pay_type_id)
  30. )
  31. engine=innodb;
  32.  
  33. drop view if exists payments_view;
  34. create view payments_view as
  35. select
  36. p.pay_id,
  37. p.pay_date,
  38. p.pay_type_id,
  39. pt.name as pay_type_name,
  40. p.amount,
  41. c.cust_id,
  42. c.email
  43. from
  44. customers c
  45. inner join payments p on c.cust_id = p.cust_id
  46. inner join payment_types pt on p.pay_type_id = pt.pay_type_id;
  47.  
  48. delimiter ;
  49.  
  50. drop procedure if exists insert_payment;
  51.  
  52. delimiter #
  53.  
  54. create procedure insert_payment
  55. (
  56. in p_email varchar(512),
  57. in p_pay_type_id tinyint unsigned,
  58. in p_amount decimal(10,2)
  59. )
  60. begin
  61.  
  62. declare v_cust_id int unsigned default 0;
  63.  
  64. if not exists (select 1 from customers where email = p_email) then
  65. insert into customers (email) values (p_email);
  66. set v_cust_id = last_insert_id();
  67. else
  68. select cust_id into v_cust_id from customers where email = p_email;
  69. end if;
  70.  
  71. insert into payments (cust_id, pay_type_id, amount)
  72. values (v_cust_id, p_pay_type_id, p_amount);
  73.  
  74. select last_insert_id() as new_pay_id;
  75.  
  76. end#
  77.  
  78. create trigger payments_before_ins_trig before insert on payments
  79. for each row
  80. begin
  81. set new.pay_date = now();
  82.  
  83. update customers set total_amount_paid = total_amount_paid + new.amount
  84. where cust_id = new.cust_id;
  85. end#
  86.  
  87. delimiter ;
  88.  
  89. insert into payment_types (name) values ('visa'),('mastercard'),('cash');
  90.  
  91. insert into customers (email) values ('foo@bar.com'),('bar@foo.com'),('pants@elis.com');
  92.  
  93. call insert_payment('foo@bar.com',1,100);
  94. call insert_payment('bar@foo.com',2,200);
  95. call insert_payment('pants@elis.com',3,300);
  96. call insert_payment('another@customer.com',1,400);
  97. call insert_payment('another@customer.com',2,500);
  98.  
  99.  
  100. mysql> select * from payment_types order by pay_type_id;
  101. +-------------+------------+
  102. | pay_type_id | name |
  103. +-------------+------------+
  104. | 1 | visa |
  105. | 2 | mastercard |
  106. | 3 | cash |
  107. +-------------+------------+
  108. 3 rows in set (0.00 sec)
  109.  
  110. mysql> select * from customers order by cust_id;
  111. +---------+----------------+-------------------+
  112. | cust_id | email | total_amount_paid |
  113. +---------+----------------+-------------------+
  114. | 1 | foo@bar.com | 600.00 |
  115. | 2 | bar@foo.com | 900.00 |
  116. | 3 | pants@elis.com | 600.00 |
  117. +---------+----------------+-------------------+
  118. 3 rows in set (0.00 sec)
  119.  
  120. mysql> select * from payments order by pay_id;
  121. +--------+---------+-------------+---------------------+--------+
  122. | pay_id | cust_id | pay_type_id | pay_date | amount |
  123. +--------+---------+-------------+---------------------+--------+
  124. | 1 | 1 | 1 | 2011-03-19 01:04:49 | 100.00 |
  125. | 2 | 1 | 2 | 2011-03-19 01:04:49 | 200.00 |
  126. | 3 | 1 | 3 | 2011-03-19 01:04:49 | 300.00 |
  127. | 4 | 2 | 3 | 2011-03-19 01:04:49 | 400.00 |
  128. | 5 | 2 | 2 | 2011-03-19 01:04:49 | 500.00 |
  129. | 6 | 3 | 1 | 2011-03-19 01:04:49 | 600.00 |
  130. +--------+---------+-------------+---------------------+--------+
  131. 6 rows in set (0.00 sec)
  132.  
  133. mysql> select * from payments_view order by pay_id desc;
  134. +--------+---------------------+-------------+---------------+--------+---------+----------------+
  135. | pay_id | pay_date | pay_type_id | pay_type_name | amount | cust_id| email |
  136. +--------+---------------------+-------------+---------------+--------+---------+----------------+
  137. | 6 | 2011-03-19 01:04:49 | 1 | visa | 600.00 | 3| pants@elis.com |
  138. | 5 | 2011-03-19 01:04:49 | 2 | mastercard | 500.00 | 2| bar@foo.com |
  139. | 4 | 2011-03-19 01:04:49 | 3 | cash | 400.00 | 2| bar@foo.com |
  140. | 3 | 2011-03-19 01:04:49 | 3 | cash | 300.00 | 1| foo@bar.com |
  141. | 2 | 2011-03-19 01:04:49 | 2 | mastercard | 200.00 | 1| foo@bar.com |
  142. | 1 | 2011-03-19 01:04:49 | 1 | visa | 100.00 | 1| foo@bar.com |
  143. +--------+---------------------+-------------+---------------+--------+---------+----------------+
  144. 6 rows in set (0.00 sec)
Add Comment
Please, Sign In to add comment