title: Create trigger to check foreign key value in MySQL
body: <p>I have a question regarding a trigger I want to create. I have 2 tables:
  one for the receipt a customer takes, displaying which products he bought and how many of them, and one for the products with PK the barcode that passes as FK to table 'receipt', and also how many items are in stock.</p>
  <p>Table 1 (receipt)</p>
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE IF NOT EXISTS `database_store`.`receipt` (
  `Barcode_fkey` INT NOT NULL,
  `Date_time_fkey` DATETIME NOT NULL,
  `Pieces` INT NOT NULL,
  PRIMARY KEY (`Date_time_fkey`),
  CONSTRAINT `fk_receipt_product1`
      FOREIGN KEY (`Barcode_fkey`)
      REFERENCES `database_store`.`product` (`Barcode`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
  CONSTRAINT `fk_receipt_transactions1`
      FOREIGN KEY (`Date_time_fkey`)
      REFERENCES `database_store`.`transactions` (`Date_time`)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION)
  </code></pre>
  <p>**I don't display the products table, because it is unnecessary.</p>
  <p>The trigger I want to create is that before inserting at the receipt table I want to check that the amount is in stock (stock &gt; receipt.amount). How can I join these tables inside of the trigger to check the stock value?</p>