{"id":801,"date":"2010-03-11T11:58:16","date_gmt":"2010-03-11T11:58:16","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=801"},"modified":"2010-03-11T12:15:38","modified_gmt":"2010-03-11T12:15:38","slug":"mysql-triggers","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/11\/mysql-triggers\/","title":{"rendered":"MySQL &#8211; triggers"},"content":{"rendered":"<p>In MySQL version 5 onwards you can use <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/triggers.html\">Triggers<\/a>, triggers are a nice was of checking values within a insert\/update\/delete process.  A trigger happens either just before the insert actually happens into the database, or just after<\/p>\n<p>Lets say that you create a table as below<\/p>\n<pre lang=\"sql\">\r\ncreate table testTable \r\n(id int not null auto_increment, \r\n  guid varchar(36), \r\n  name varchar(100), \r\nprimary key (id));\r\n<\/pre>\n<p>and then if you want to insert a name into the name column you could do<\/p>\n<pre lang=\"sql\">\r\ninsert into testTable (name) values (\"thename\");\r\n<\/pre>\n<p>but you are not inserting the anything into the guid column because you can force a value into that column with a trigger (the id is already creating a value with the primary key, auto_increment).<\/p>\n<p>To create a trigger you need to set the delimiter to &#8220;\/\/&#8221; because within sql you need to use the &#8220;;&#8221; which is the standard delimiter (when the MySQL will try and execute the query)<\/p>\n<pre lang=\"sql\">\r\ndelimiter \/\/\r\nCREATE TRIGGER testTable_insert \r\nBEFORE INSERT ON 'testTable' \r\nFOR EACH ROW \r\nBEGIN \r\n    SET NEW.guid = uuid(); \r\nEND;\r\ndelimiter ;\r\n<\/pre>\n<p>&#8220;CREATE TRIGGER&#8221; is just like creating a table, apart from you are creating a trigger, so the next value is the trigger name, normally it is the table name with what you are doing, e.g. inserting<\/p>\n<p>&#8220;BEFORE INSERT ON&#8221; means before you actually insert the data into the database (it is in a hold area as such). the next value is the actual table that you are linking this trigger to.<\/p>\n<p>&#8220;FOR EACH ROW&#8221; means each row of the insert, since you can insert x amount of lines into a table.<\/p>\n<p>the sql code is between the &#8220;BEGIN&#8221; and &#8220;END&#8221;, and all is what is being set is the guid column within the &#8220;NEW&#8221; (this is the new row to be inserted) and setting that value to <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/miscellaneous-functions.html#function_uuid\">uuid<\/a>() which is a mysql function.<\/p>\n<p>so before you would have inserted into a table and the table would have looked like<\/p>\n<table>\n<tr>\n<td width=\"100\">\n<td width=\"100\">\n<td width=\"100\">\n<\/tr>\n<tr>\n<td>ID<\/td>\n<td>GUID<\/td>\n<td>Name<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td><\/td>\n<td>thename<\/td>\n<\/tr>\n<\/table>\n<p>but once the trigger is in place and you did the same process again of inserting a new name<\/p>\n<pre lang=\"sql\">\r\ninsert into testTable (name) values (\"a new name\");\r\n<\/pre>\n<table>\n<tr>\n<td width=\"100\">\n<td width=\"100\">\n<td width=\"100\">\n<\/tr>\n<tr>\n<td>ID<\/td>\n<td>GUID<\/td>\n<td>Name<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td><\/td>\n<td>thename<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>3ace82c-2cf1-11df-b1c3-00a0d1a1240a<\/td>\n<td>a new name<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>In MySQL version 5 onwards you can use Triggers, triggers are a nice was of checking values within a insert\/update\/delete process. A trigger happens either just before the insert actually happens into the database, or just after Lets say that you create a table as below create table testTable (id int not null auto_increment, guid &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/11\/mysql-triggers\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">MySQL &#8211; triggers<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[22,140],"class_list":["post-801","post","type-post","status-publish","format-standard","hentry","category-sql","tag-mysql","tag-triggers"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=801"}],"version-history":[{"count":3,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/801\/revisions"}],"predecessor-version":[{"id":804,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/801\/revisions\/804"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}