I don't know why your DESPATCH table has the PROD NAME column - that ought to be derivable by a foreign key lookup on PRODUCT via PROD ID. It definitely does need a link to SALES, otherwise how will the dispatcher be able to find the address to send it too. I also think it needs a DATE_DISPATCHED column, which will only be populated after the order has been mailed.
You don't say database you're using. I'll use Oracle because it's the one I'm most familiar with.
create or replace trigger sale_trig
after insert on sales for each row
begin
insert into despatch
(desp_id
, shelfLoc
, quantity
, prod_id
, prod_name
, sales_id)
values
(despatch_sequence.nextval
, dbms_random.string('U', 5)
, :new.no_of_prods
, :new.prod_id
, :new.prod_name
, :new.sales_id);
end;
/
Note that I have had to make some assumptions about your data model because you haven't provided sufficient information:
- desp_id is populated with a sequence
- shelfloc is populated through some
kind of lookup
- sales.no_of_prods maps to
despatch.quantity
Edit: whilst retagging your question the Update tag made me re-read your question. You say you want to update the DESPATCH table, but I don't understand why. Why would you have a DESPATCH record before an order has been placed? So what is there to update?
Edit 2: I have revised the trigger code to reflect your response.