MrCBofBCinTX
Technical User
I have an existing database of products with prices and vendors,etc.
A script updates prices 1-4 times a month.
I now want to add a set of tables to describe an assembly of parts to make specific project items like a floor or wall of a particular size
Each project, like a 12' x 20' Floor will have an item from products table with a quantity. I made a subtotal column, which uses a function and trigger to calculate:
This seems to work OK. I am new to functions and triggers so please criticize any errors.
But I also need this subtotal to be recalculated when the products table price is updated. I can't seem to get a suitable set of subqueries put together for this.
A script updates prices 1-4 times a month.
I now want to add a set of tables to describe an assembly of parts to make specific project items like a floor or wall of a particular size
Each project, like a 12' x 20' Floor will have an item from products table with a quantity. I made a subtotal column, which uses a function and trigger to calculate:
Code:
CREATE FUNCTION sum_assembly_part_subtotal() RETURNS "trigger" AS '
BEGIN
IF NEW.assembly_part_subtotal IS NULL THEN
NEW.assembly_part_subtotal := NEW.assembly_part_quantity*(SELECT price FROM products WHERE products.product_id=NEW.assembly_part_product_id);
END IF;
RETURN NEW;
END;'
LANGUAGE plpgsql;
Code:
CREATE TRIGGER sum_assembly_part_subtotal_trigger
BEFORE INSERT OR UPDATE OR DELETE ON assemblies_parts
FOR EACH ROW
EXECUTE PROCEDURE sum_assembly_part_subtotal();
This seems to work OK. I am new to functions and triggers so please criticize any errors.
But I also need this subtotal to be recalculated when the products table price is updated. I can't seem to get a suitable set of subqueries put together for this.
Code:
\d products
Table "public.products"
Column | Type | Modifiers
---------------------+------------------------+-----------------------------------------------------
----------
product_id | integer | not null default nextval('products_product_id_seq'::
regclass)
product_description | text |
sku | character varying(100) |
up_date | date | not null default now()
class | character varying(100) |
subclass | character varying(100) |
vendor_id | integer | not null
vendor_name | character varying(100) | not null
price | numeric(10,2) | not null
model | character varying(100) |
product_notes | text |
check_days | text | not null default '2'::text
product_url | text |
Indexes:
"products_pkey" PRIMARY KEY, btree (product_id)
Foreign-key constraints:
"products_vendor_id_fkey" FOREIGN KEY (vendor_id) REFERENCES vendors(vendor_id)
"products_vendor_name_fkey" FOREIGN KEY (vendor_name) REFERENCES vendors(vendor_name)
Referenced by:
TABLE "assemblies_parts" CONSTRAINT "assemblies_parts_product_id_fkey" FOREIGN KEY (assembly_par
t_product_id) REFERENCES products(product_id)
Code:
\d assemblies_parts
Table "public.assemblies_parts"
Column | Type | Modifiers
---------------------------+---------------+--------------------------------------------------------
---------------------
assembly_part_id | integer | not null default nextval('assemblies_parts_assembly_par
t_id_seq'::regclass)
assembly_part_assembly_id | integer | not null
assembly_part_name | text | not null
assembly_part_description | text |
assembly_part_class | text |
assembly_part_subclass | text |
assembly_part_notes | text |
assembly_part_url | text |
assembly_part_quantity | integer | not null
assembly_part_product_id | integer | not null
assembly_part_subtotal | numeric(10,2) | not null
assembly_part_update | date | not null default now()
Indexes:
"assemblies_parts_pkey" PRIMARY KEY, btree (assembly_part_id)
Foreign-key constraints:
"assemblies_parts_product_id_fkey" FOREIGN KEY (assembly_part_product_id) REFERENCES products(pr
oduct_id)
"assembly_part_assembly_id_fkey" FOREIGN KEY (assembly_part_assembly_id) REFERENCES assemblies(a
ssembly_id)
Triggers:
sum_assembly_part_subtotal_trigger BEFORE INSERT OR DELETE OR UPDATE ON assemblies_parts FOR EAC
H ROW EXECUTE PROCEDURE sum_assembly_part_subtotal()