Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

3.

7 Trigger Procedure
(1)The trigger paymentinfo_trg executed automatically AFTER UPDATE operation is

performed on table payment. Futhermore the updated record is inserted into table

payment_info.

mysql> create table payment_info (payId varchar(20),new_payAmt int,old_payAmt int);

Query OK, 0 rows affected (0.44 sec)

mysql> delimiter //

mysql> create trigger paymentinfo_trg after update on payment for each row begin insert into
payment_info values(old.payId,new.payAmt,old.payAmt);end //

Query OK, 0 rows affected (0.43 sec)

mysql> delimiter ;

mysql> update payment set payAmt=10000 where payAmt=0;

Query OK, 1 row affected (0.38 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from payment_info;

+-------+------------+------------+

| payId | new_payAmt | old_payAmt |

+-------+------------+------------+

| P3 | 10000 | 0|

+-------+------------+------------+

1 row in set (0.00 sec)


(2) The trigger cust_trg executed automatically AFTER INSERT operation is

performed on table customer. Futhermore the new inserted record is inserted into table

cust_info.

mysql> create table cust_info(custId varchar(20),firstName varchar(20),lastName


varchar(20),custNrc varchar(50),custPhone varchar(50),custAddress varchar(50),custEmail
varchar(50),custPassword varchar(50),modifiedDate Timestamp);

Query OK, 0 rows affected (0.20 sec)

mysql> delimiter //

mysql> create trigger cust_trg after insert on customer for each row begin insert into
cust_info(custId,firstName,lastName,custNrc,custPhone,custAddress,custEmail,custPassword)
values(new.custId,new.firstName,new.lastName,new.custNrc,new.custPhone,new.custAddress,n
ew.custEmail,new.custPassword);end //

Query OK, 0 rows affected (0.14 sec)

mysql> delimiter ;

mysql> insert into customer values('C7','Htet Nay','Chi


Lin','9/ZaYaTha(N)004442','09964111237','Mingalardone','htet.mm2020@gmail.com','htet');

Query OK, 1 row affected (0.06 sec)

mysql> select * from cust_info;

+--------+-----------+----------+--------------------+-------------+--------------+-----------------------
+--------------+---------------------+

| custId | firstName | lastName | custNrc | custPhone | custAddress | custEmail |


custPassword | modifiedDate |

+--------+-----------+----------+--------------------+-------------+--------------+-----------------------
+--------------+---------------------+

| C7 | Htet Nay | Chi Lin | 9/ZaYaTha(N)004442 | 09964111237 | Mingalardone |


htet.mm2020@gmail.com | htet | 2019-02-20 13:15:14 |
+--------+-----------+----------+--------------------+-------------+--------------+-----------------------
+--------------+---------------------+

1 row in set (0.00 sec)

(3) The trigger seat_trg executed automatically BEFORE INSERT operation is

performed on table payment. If the number of seat is invalid,set the invalid seat number

to zero in the payment table.

mysql> insert into payment values('P5','C5',-1,10000,'20.9.19');

Query OK, 1 row affected (0.06 sec)

mysql> select * from payment;

+-------+--------+----------+--------+---------+

| payId | custId | noOfSeat | payAmt | payDate |

+-------+--------+----------+--------+---------+

| P1 | C1 | 2 | 30000 | 13.9.19 |

| P2 | C2 | 2 | 25000 | 13.9.19 |

| P3 | C3 | 0 | 10000 | 3.9.19 |

| P4 | C4 | 1 | 10000 | 3.9.19 |

| P5 | C5 | -1 | 10000 | 20.9.19 |

+-------+--------+----------+--------+---------+

5 rows in set (0.00 sec)

mysql> delimiter //

mysql> create trigger seat_trg before insert on payment for each row begin if new.noOfSeat < 0
then set new.noOfSeat=0; end if;end //

Query OK, 0 rows affected (0.17 sec)


mysql> delimiter ;

mysql> insert into payment values('P6','C5',-1,10000,'20.9.19');

Query OK, 1 row affected (0.08 sec)

mysql> select * from payment;

+-------+--------+----------+--------+---------+

| payId | custId | noOfSeat | payAmt | payDate |

+-------+--------+----------+--------+---------+

| P1 | C1 | 2 | 30000 | 13.9.19 |

| P2 | C2 | 2 | 25000 | 13.9.19 |

| P3 | C3 | 0 | 10000 | 3.9.19 |

| P4 | C4 | 1 | 10000 | 3.9.19 |

| P5 | C5 | -1 | 10000 | 20.9.19 |

| P6 | C5 | 0 | 10000 | 20.9.19 |

+-------+--------+----------+--------+---------+

(4) The trigger deletecust_trg executed automatically AFTER DELETE operation is

performed on table customer. Futhermore the deleted record is deleted from table

cust_info.

mysql> select * from cust_info;

+--------+-----------+----------+--------------------+-------------+--------------+----------------
+--------------+---------------------+

| custId | firstName | lastName | custNrc | custPhone | custAddress | custEmail |


custPassword | modifiedDate |
+--------+-----------+----------+--------------------+-------------+--------------+----------------
+--------------+---------------------+

| C7 | Htet Nay | Chi Lin | 9/ZaYaTha(N)004442 | 09964111237 | Mingalardone |


htet@gmail.com | htet | 2019-02-20 20:37:29 |

+--------+-----------+----------+--------------------+-------------+--------------+----------------
+--------------+---------------------+

1 row in set (0.00 sec)

mysql> delimiter //

mysql> create trigger deletecust_trg after delete on customer for each row begin delete from
cust_info where custId=old.custId;end //

Query OK, 0 rows affected (0.14 sec)

mysql> delimiter ;

mysql> delete from customer where custId='C7';

Query OK, 1 row affected (0.37 sec)

mysql> select * from customer;

+--------+------------+------------+---------------------+-------------+-------------+----------------------
+--------------+

| custId | firstName | lastName | custNrc | custPhone | custAddress | custEmail |


custPassword |

+--------+------------+------------+---------------------+-------------+-------------+----------------------
+--------------+

| C1 | Hsu | Myat | 10/ZaYaTha(N)004442 | 09964111237 | NayPyiTaw |


hsu1234@gmail.com | hsu |

| C2 | Hanni | Myint Kyaw | 10/MaDaNa(N)223601 | 09978962579 | Mawlamyine |


hanni1234@gmail.com | honey |
| C3 | Su Thinzar | Htay | 10/YaPaTha(N)444321 | 09975553508 | ShwePyiThar |
su4321@gmail.com | thinzar |

| C4 | May Thu | Aung | 10/KaTaNa(N)134606 | 09263222340 | TarMwe |


maythu5678@gmail.com | maythu |

| C5 | San Thaw | Tar Oo | 12/MaBaNa(N)174832 | 09788583194 | HmawBi |


san4567@gmail.com | thawtar |

| C6 | Myat | Theingi | 12/ThaLaNa(N)128129 | 09775591926 | Thanlyin |


myat1999@gmail.com | theingi |

+--------+------------+------------+---------------------+-------------+-------------+----------------------
+--------------+

6 rows in set (0.00 sec)

mysql> select * from cust_info;

Empty set (0.00 sec)

(5) The trigger delCust_trg executed automatically BEFORE DELETE operation is

performed on table customer. Futhermore the deleted record is recorded into table

delCust_info with specific date and time.

mysql> create table delCust_info(custId varchar(20),firstName varchar(20),lastName


varchar(20),custNrc varchar(20),deleteDate Timestamp);

Query OK, 0 rows affected (0.16 sec)

mysql> delimiter //

mysql> create trigger delCust_trg before delete on customer for each row begin insert into
delCust_info(custId,firstName,lastName,custNrc)
values(old.custId,old.firstName,old.lastName,old.custNrc);end //

Query OK, 0 rows affected (0.44 sec)


mysql> delimiter ;

mysql> select * from customer;

+--------+------------+------------+---------------------+-------------+-------------+----------------------
+--------------+

| custId | firstName | lastName | custNrc | custPhone | custAddress | custEmail |


custPassword |

+--------+------------+------------+---------------------+-------------+-------------+----------------------
+--------------+

| C1 | Hsu | Myat | 10/ZaYaTha(N)004442 | 09964111237 | NayPyiTaw |


hsu1234@gmail.com | hsu |

| C2 | Hanni | Myint Kyaw | 10/MaDaNa(N)223601 | 09978962579 | Mawlamyine |


hanni1234@gmail.com | honey |

| C3 | Su Thinzar | Htay | 10/YaPaTha(N)444321 | 09975553508 | ShwePyiThar |


su4321@gmail.com | thinzar |

| C4 | May Thu | Aung | 10/KaTaNa(N)134606 | 09263222340 | TarMwe |


maythu5678@gmail.com | maythu |

| C5 | San Thaw | Tar Oo | 12/MaBaNa(N)174832 | 09788583194 | HmawBi |


san4567@gmail.com | thawtar |

| C6 | Myat | Theingi | 12/ThaLaNa(N)128129 | 09775591926 | Thanlyin |


myat1999@gmail.com | theingi |

| C7 | Kyaw | Kyaw | 12/ThaLaNa(N)128542 | 09783044562 | Thanlyin |


kyaw@gmail.com | kyaw |

+--------+------------+------------+---------------------+-------------+-------------+----------------------
+--------------+

7 rows in set (0.36 sec)

mysql> delete from customer where custId='C7';

Query OK, 1 row affected (0.37 sec)


mysql> select * from delCust_info;

+--------+-----------+----------+---------------------+---------------------+

| custId | firstName | lastName | custNrc | deleteDate |

+--------+-----------+----------+---------------------+---------------------+

| C7 | Kyaw | Kyaw | 12/ThaLaNa(N)128542 | 2019-02-27 21:16:55 |

+--------+-----------+----------+---------------------+---------------------+

1 row in set (0.00 sec)

You might also like