{"id":1531,"date":"2022-11-19T00:04:03","date_gmt":"2022-11-19T00:04:03","guid":{"rendered":"\/blog\/?p=1531"},"modified":"2022-11-19T00:40:26","modified_gmt":"2022-11-19T00:40:26","slug":"mysql-trigger","status":"publish","type":"post","link":"\/blog\/index.php\/mysql-trigger\/","title":{"rendered":"Mysql trigger"},"content":{"rendered":"\n<p>If you want to trigger an action to another table you can create a trigger:<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><code>CREATE TRIGGER `update_menus` AFTER INSERT ON `usuarios` FOR EACH ROW insert into sys_user_roles set sys_roles_id=(select sys_roles_id from sys_roles where roles_name=NEW.tipo),user_id=NEW.user_id<br><br>CREATE TRIGGER `update_totals_after_delete` AFTER DELETE ON `pedidos_detalle`\u00a0<br>FOR EACH ROW\u00a0<br>update pedidos set subtotal = (select sum(importe) from pedidos_detalle where folio=OLD.folio)\u00a0<br>where folio=OLD.folio<br><br>CREATE TRIGGER `update_totals_after_insert` AFTER INSERT ON `pedidos_detalle`\u00a0<br>FOR EACH ROW\u00a0<br>update pedidos set subtotal = (select sum(importe) from pedidos_detalle where folio=NEW.folio)\u00a0<br>where folio=NEW.folio<\/code><\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you want to trigger an action to another table you can create a trigger: CREATE TRIGGER `update_menus` AFTER INSERT ON `usuarios` FOR EACH ROW insert into sys_user_roles set sys_roles_id=(select sys_roles_id from sys_roles where roles_name=NEW.tipo),user_id=NEW.user_id CREATE TRIGGER `update_totals_after_delete` AFTER DELETE ON `pedidos_detalle`\u00a0FOR EACH ROW\u00a0update pedidos set subtotal = (select sum(importe) from pedidos_detalle where folio=OLD.folio)\u00a0where folio=OLD.folio [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1531","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1531"}],"version-history":[{"count":3,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1531\/revisions"}],"predecessor-version":[{"id":1534,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1531\/revisions\/1534"}],"wp:attachment":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1531"},{"taxonomy":"post_tag","embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}