source: database/Advanced Database Developement/Triggers/Prevention of deleting stores with existing orders or reports.txt@ 6c7cfa6

main
Last change on this file since 6c7cfa6 was 62b2964, checked in by Klimentina Efremova <klimentina08642@…>, 2 weeks ago

Project Handcraft Marketplace

  • Property mode set to 100644
File size: 738 bytes
Line 
1CREATE OR REPLACE FUNCTION prevent_store_deletion()
2RETURNS TRIGGER
3LANGUAGE plpgsql
4AS $$
5BEGIN
6 -- Check whether the store has existing reports
7 IF EXISTS ( SELECT 1 FROM report WHERE store_id = OLD.store_id ) THEN
8 RAISE EXCEPTION 'Store % cannot be deleted because it has existing reports.', OLD.store_id;
9 END IF;
10 -- Check whether the store has products associated with it
11 IF EXISTS ( SELECT 1 FROM sells WHERE store_id = OLD.store_id ) THEN
12 RAISE EXCEPTION 'Store % cannot be deleted because it has existing product records.', OLD.store_id;
13 END IF;
14 RETURN OLD;
15END;
16$$;
17
18CREATE TRIGGER trg_prevent_store_deletion
19BEFORE DELETE
20ON store
21FOR EACH ROW
22EXECUTE FUNCTION prevent_store_deletion();
Note: See TracBrowser for help on using the repository browser.