1

Getting error after error. Basically I am trying to set a columns value to 1 in my products table automatically if upon update of the product_stock table the column available is greater than 0 (meaning, at least one in stock).

MPN is both a unique and foreign key in my products table, so as long a positive value in the column available in the table product_stock the in_stock value for the mpn in the products table should be set to 1.

Two tables I'm working with:

1

CREATE TABLE `products` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `mpn` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `in_stock` int(1) NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`),
 UNIQUE KEY `mpn` (`mpn`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

2

CREATE TABLE `product_stock` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `mpn` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `size` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `available` int(11) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `product_stock_ibfk_1` (`mpn`),
 CONSTRAINT `product_stock_ibfk_1` FOREIGN KEY (`mpn`) REFERENCES `products` (`mpn`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

And one variation of my trigger

DELIMITER $$

    CREATE TRIGGER ps_update AFTER UPDATE ON `product_stock`
    FOR EACH ROW BEGIN
      IF NEW.available > 0 THEN
            SET products.in_stock = 1;
      ELSE
            SET products.in_stock = 0;
      END IF;
    END$$

DELIMITER ;

Error code

1193 - Unknown system variable 'in_stock'

Brian Bruman
  • 883
  • 12
  • 28

1 Answers1

1

You cannot update value in another table using SET alone. You need to use proper UPDATE statement to do so.

I have also added more conditions, so that it does not fire UPDATE query every time. It will fire UPDATE only when there is a change in the in_stock value required.

DELIMITER $$
CREATE TRIGGER ps_update AFTER UPDATE ON `product_stock`

    FOR EACH ROW BEGIN

      -- update only when there is a change in the available
      IF NEW.available <> OLD.available THEN 

        -- update only when item becomes in_stock
        IF NEW.available > 0 AND OLD.available <= 0 THEN 
            UPDATE products 
            SET products.in_stock = 1 
            WHERE products.mpn = NEW.mpn;

        -- update only when item becomes out_stock
        ELSEIF NEW.available <= 0 AND OLD.available > 0 THEN 
            UPDATE products 
            SET products.in_stock = 0 
            WHERE products.mpn = NEW.mpn;
        END IF;
      END IF;
   END $$
DELIMITER ;
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
  • Didn't even mean to include that word :) Thanks for pointing that out. Updated my question with the new error message.. – Brian Bruman Nov 10 '18 at 12:16
  • @BrianBruman check the updated answer . You need to use update query – Madhur Bhaiya Nov 10 '18 at 12:18
  • Awesome! It works. Only thing missing is `DELIMITER $$` in the beginning for the query to process it and get the trigger enabled. But yes, just tested it and it works. Thanks much. – Brian Bruman Nov 10 '18 at 12:20
  • @BrianBruman check the further updated answer. I have optimized it. – Madhur Bhaiya Nov 10 '18 at 12:23
  • Perfect man, love it! Now I'm wondering if this trigger will malfunction if the *last* size `available` row is `0` even if the mpn has stock (`> 0`) for previous rows.. Got me thinking. I'm quite new at triggers, going to look into if its possible to break out of one if a condition is met or if there's some other logic I can implement :) – Brian Bruman Nov 10 '18 at 12:38
  • 1
    And of course it does. Even if one size/row has 0 available it will revert back to out of stock (even if other sizes/rows have stock). You got me well on track though thanks again. – Brian Bruman Nov 10 '18 at 12:42