-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSQL11-Backup_Triggers
More file actions
78 lines (57 loc) · 1.86 KB
/
Copy pathSQL11-Backup_Triggers
File metadata and controls
78 lines (57 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* BACKUP TRIGGERS AND DATABASE COMMUNICATION */
-- Create database and table --
create database store;
use store;
create table product(
idProduct int primary key auto_increment,
nameProduct varchar (30),
priceProduct float (10,2)
);
-- Create database and table --
create database store_backup;
use store_backup;
create table product_bkp(
idbkpProduct int primary key auto_increment,
idProductBKP int,
nameProductBKP varchar (30),
priceProductBKP float (10,2)
);
-- Create trigger --
use store;
delimiter //
create trigger T_bkp_product
after insert on product /* after insert, data goes to backup */
for each row
begin
insert into store_backup.product_bkp
values (null,new.idProduct,new.nameProduct,new.priceProduct);
end
//
-- Insert Values
insert into product
values (null, 'mouse','11.99'),
(null, 'keyboard','21.99'),
(null, 'webcam','31.99'),
(null, 'mousepad','3.99')
//
-- Results --
use store //
select * from product //
+-----------+-------------+--------------+
| idProduct | nameProduct | priceProduct |
+-----------+-------------+--------------+
| 1 | mouse | 11.99 |
| 2 | keyboard | 21.99 |
| 3 | webcam | 31.99 |
| 4 | mousepad | 3.99 |
+-----------+-------------+--------------+
use store_backup //
select * from product_bkp //
+--------------+--------------+----------------+-----------------+
| idbkpProduct | idProductBKP | nameProductBKP | priceProductBKP |
+--------------+--------------+----------------+-----------------+
| 1 | 1 | mouse | 11.99 |
| 2 | 2 | keyboard | 21.99 |
| 3 | 3 | webcam | 31.99 |
| 4 | 4 | mousepad | 3.99 |
+--------------+--------------+----------------+-----------------+