Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE discount_group (
- customer_group_id INTEGER DEFAULT NULL,
- customer_id INTEGER DEFAULT NULL,
- product_group_id INTEGER DEFAULT NULL,
- product_id INTEGER DEFAULT NULL,
- discount REAL NOT NULL
- );
- ALTER TABLE discount_group
- ADD CONSTRAINT customer_group_discount_group FOREIGN KEY (customer_group_id)
- REFERENCES customer_group(id) ON UPDATE CASCADE ON DELETE CASCADE;
- ALTER TABLE discount_group
- ADD CONSTRAINT customer_discount_group FOREIGN KEY (customer_id)
- REFERENCES customer(id) ON UPDATE CASCADE ON DELETE CASCADE;
- ALTER TABLE discount_group
- ADD CONSTRAINT product_group_discount_group FOREIGN KEY (product_group_id)
- REFERENCES product_group(id) ON UPDATE CASCADE ON DELETE CASCADE;
- ALTER TABLE discount_group
- ADD CONSTRAINT product_discount_group FOREIGN KEY (product_id)
- REFERENCES product(id) ON UPDATE CASCADE ON DELETE CASCADE;
- CREATE OR REPLACE FUNCTION add_discount(
- customer_group_id INTEGER,
- customer_id INTEGER,
- product_group_id INTEGER,
- product_id INTEGER,
- discount REAL
- )
- RETURNS INTEGER AS $$
- DECLARE points INTEGER DEFAULT 0;
- BEGIN
- IF customer_group_id IS NOT NULL OR customer_id IS NOT NULL THEN
- points := points + 1;
- END IF;
- IF product_group_id IS NOT NULL OR product_id IS NOT NULL THEN
- points := points +1;
- END IF;
- IF points < 2 THEN
- RAISE EXCEPTION 'Either no customer_group_id/customer_id
- or product_group_id/product_id was specified';
- END IF;
- RETURN 0;
- END;
- $$ LANGUAGE plpgsql;
Add Comment
Please, Sign In to add comment