0

I'm having trouble finding a solution to my problem. I need to be able to create a TRIGGER/PROCEDURE/FUNCTION (whichever is most applicable) for the following task.

I would like to calculate the values of multiple columns and place the result of the calculations into columns within the same MYSQL table. I would like the calculation to take place every time there is an action performed which either UPDATES or INSERTS data into/within the database table.

I've created this PROCEDURE to demonstrate my approach (this procedure currently throws syntax errors after UPDATE product SET product.bawtry_stock = )

MYSQL

DELIMITER $$

    CREATE PROCEDURE update_stock()
      BEGIN
        ON `product` 
          UPDATE product SET product.bawtry_stock =

            kids_uk_j_105 + kids_c_17 + kids_c_18 + kids_c_19 + 
            kids_c_20 + kids_c_21,


            quantity_c_size_26 + quantity_c_size_26_con_b,

            product.chain_stock =

            // as above with chain size suffixes

            product.totalstock = bawtry_stock + chain_stock,
            product.bawtry_value = bawtry_stock * wholesale_price,
            product.chain_value = chain_stock * wholesale_price,
            product.totalvalue = chain_value + bawtry_value;
  END$$

DELIMITER ;

**MYSQL Table **

TBL NAME = 'product' 
TBL COLUMNS = bawtry_stock, 
              chain_stock, 
              totalstock, 
              bawtry_value,
              chain_value,
              totalvalue, 
              product_id, (PKEY)
              (together with all of the kids and quantity size columns) 

So each time an UPDATE or INSERT action is carried out on the product table - I need all of the values within the columns listed to be calculated and entered into bawtry_stock, chain_stock etc.

If this isn't possible, then perhaps create a FUNCTION/PROCEDURE that could be called via PHP that I would run as a CRONJOB.

If anybody could offer some advice - it would be appreciated. Thanks

EDIT

Just to provide some further information - I already tried this as a TRIGGER using similar syntax :

TRIGGER

DELIMITER $$

CREATE
    TRIGGER `update_stock`
AFTER UPDATE ON `product` 
FOR EACH ROW BEGIN
            UPDATE product
            SET bawtry_stock =

 // ... same body logic as above

  END$$

DELIMITER ;

This produced the MySQL Trigger: ERROR 1442 (HY000): Cant update table 'tbl' in stored function/trigger because it is already used by statement which invoked this stored function/trigger error.

I looked into this and came across this article LINK

aphextwix
  • 1,838
  • 3
  • 21
  • 27
  • "But why can't I work with that schema?" – Mitch Wheat Jul 22 '14 at 11:56
  • [Triggers can do that](http://dev.mysql.com/doc/refman/5.6/en/triggers.html). Now please explain where you are stuck. – RandomSeed Jul 22 '14 at 12:02
  • @RandomSeed - please see further edits above to explain where I've become stuck. – aphextwix Jul 22 '14 at 12:10
  • possible duplicate of [MySQL - Trigger for updating same table after insert](http://stackoverflow.com/questions/12877732/mysql-trigger-for-updating-same-table-after-insert) -- you can't update arbitrary rows in the table that invoked the trigger, but [you can modify the row(s) that were being changed when the trigger started](http://stackoverflow.com/a/17047386/1446005). – RandomSeed Jul 22 '14 at 12:21
  • @RandomSeed - so the best thing to do here would be create a TRIGGER which modifies the row which is being altered either via the UPDATE or INSERT action. That makes sense, in that you wouldn't need to calculate each row within the table every time only one row is being modified. Could you flesh out an example showing how my trigger would work for just one row? Thanks – aphextwix Jul 22 '14 at 12:41

2 Answers2

1

You can create an other table, which holds your calculated value and product_id (which can be referred with a foreign key), add trigger to your main table for update and insert actions and update or insert your values at your new table. Whereever you are using these values you can join these two tables at select statement.

  • It's beginning to look like that is my best way of approaching this task. Thank you for the suggestion. – aphextwix Jul 22 '14 at 13:11
0

This is very complicated table, and it is not common to use tables like this. I suggest you to think how to refactor this table to separate tables with less columns.

try reading about data normalisation for relational databases. try this tutorial

Now back to your question. If u need to update the table each time the data is being changed - u need a trigger. In the trigger you refer to columns as new.col_name and old.col_name.

You will have to have 2 triggers. 1 for update, and 1 for insert . I suggest you to use before insert trigger and before update trigger.

something like:

CREATE TRIGGER update_product_stock_trg BEFORE update ON product
FOR EACH ROW SET 
    new.totalstock = new.bawtry_stock + new.chain_stock,
    new.bawtry_value = new.bawtry_stock * new.wholesale_price,
    new.chain_value = new.chain_stock * new.wholesale_price,
    new.totalvalue = new.chain_value + new.bawtry_value

    .... and so on for all your calculations.

;


CREATE TRIGGER insert_product_stock_trg BEFORE INSERT ON product
FOR EACH ROW SET 
    new.totalstock = new.bawtry_stock + new.chain_stock,
    new.bawtry_value = new.bawtry_stock * new.wholesale_price,
    new.chain_value = new.chain_stock * new.wholesale_price,
    new.totalvalue = new.chain_value + new.bawtry_value
 .... and so on for all your calculations.

;

you can read more about creating triggers in MySQL here

Tata
  • 802
  • 9
  • 19
  • I can't get this to work whatsoever. I've created the triggers as shown above and the queries were successful. However, when I try to insert/update anything into the table the following error message is shown `#1054 - Unknown column 'quantity_size_1' in 'field list'` - I've been through my code with a fine tooth comb and there's no declaration of that size in my trigger statement... I'm at a loss. Any clue? – aphextwix Jul 22 '14 at 14:17
  • Can you send me the full table declaration and the triggers you have wrote? – Tata Jul 27 '14 at 12:53