source: music/triggers/customer_deletion.sql

Last change on this file was d715225, checked in by ManuelTrajcev <manueltrajcev7@…>, 11 days ago

deleted customer log trigger

  • Property mode set to 100644
File size: 811 bytes
Line 
1DROP TRIGGER IF EXISTS trg_customer_deletion ON customer;
2DROP FUNCTION IF EXISTS customer_deletion();
3
4CREATE OR REPLACE FUNCTION customer_deletion()
5RETURNS TRIGGER AS $$
6DECLARE
7 total_spent NUMERIC(10, 2);
8 invoice_count INTEGER;
9BEGIN
10 SELECT COALESCE(SUM(total), 0), COUNT(*)
11 INTO total_spent, invoice_count
12 FROM invoice
13 WHERE customer_id = OLD.customer_id;
14
15 INSERT INTO deleted_customer_log (
16 first_name, last_name, deleted_at, total_spent, invoice_count
17 )
18 VALUES (
19 OLD.first_name, OLD.last_name, NOW(), total_spent, invoice_count
20 );
21
22 RETURN OLD;
23END;
24$$ LANGUAGE plpgsql;
25
26DROP TRIGGER IF EXISTS trg_customer_deletion ON customer;
27
28CREATE TRIGGER trg_customer_deletion
29BEFORE DELETE ON customer
30FOR EACH ROW
31EXECUTE FUNCTION customer_deletion();
Note: See TracBrowser for help on using the repository browser.