-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiger.sql
39 lines (34 loc) · 1.23 KB
/
tiger.sql
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
CREATE TABLE Log (
id serial PRIMARY KEY,
table_name varchar(50) NOT NULL,
action varchar(10) NOT NULL,
changed_at timestamp NOT NULL
);
CREATE OR REPLACE FUNCTION log_changes()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'INSERT' THEN
INSERT INTO Log (table_name, action, changed_at)
VALUES (TG_TABLE_NAME, 'INSERT', NOW());
ELSIF TG_OP = 'UPDATE' THEN
INSERT INTO Log (table_name, action, changed_at)
VALUES (TG_TABLE_NAME, 'UPDATE', NOW());
ELSIF TG_OP = 'DELETE' THEN
INSERT INTO Log (table_name, action, changed_at)
VALUES (TG_TABLE_NAME, 'DELETE', NOW());
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER product_trigger
AFTER INSERT OR UPDATE OR DELETE ON Product
FOR EACH ROW EXECUTE FUNCTION log_changes();
CREATE TRIGGER pc_trigger
AFTER INSERT OR UPDATE OR DELETE ON PC
FOR EACH ROW EXECUTE FUNCTION log_changes();
CREATE TRIGGER laptop_trigger
AFTER INSERT OR UPDATE OR DELETE ON Laptop
FOR EACH ROW EXECUTE FUNCTION log_changes();
CREATE TRIGGER printer_trigger
AFTER INSERT OR UPDATE OR DELETE ON Printer
FOR EACH ROW EXECUTE FUNCTION log_changes();