<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coding Friends &#187; triggers</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/triggers/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MySQL &#8211; triggers</title>
		<link>http://www.codingfriends.com/index.php/2010/03/11/mysql-triggers/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mysql-triggers</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/11/mysql-triggers/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 11:58:16 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[SQL (Structured Query Language)]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[triggers]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=801</guid>
		<description><![CDATA[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 &#40;id INT NOT NULL AUTO_INCREMENT, guid [...]]]></description>
			<content:encoded><![CDATA[<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>
<p>Lets say that you create a table as below</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> testTable 
<span style="color: #66cc66;">&#40;</span>id <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span> 
  guid <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
  name <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>and then if you want to insert a name into the name column you could do</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> testTable <span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;thename&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<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>
<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>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">delimiter <span style="color: #66cc66;">//</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> testTable_insert 
<span style="color: #993333; font-weight: bold;">BEFORE</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #ff0000;">'testTable'</span> 
<span style="color: #993333; font-weight: bold;">FOR</span> EACH <span style="color: #993333; font-weight: bold;">ROW</span> 
<span style="color: #993333; font-weight: bold;">BEGIN</span> 
    <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #993333; font-weight: bold;">NEW</span><span style="color: #66cc66;">.</span>guid <span style="color: #66cc66;">=</span> uuid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
<span style="color: #993333; font-weight: bold;">END</span>;
delimiter ;</pre></div></div>

<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>
<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>
<p>&#8220;FOR EACH ROW&#8221; means each row of the insert, since you can insert x amount of lines into a table.</p>
<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>
<p>so before you would have inserted into a table and the table would have looked like</p>
<table>
<tr>
<td width="100">
<td width="100">
<td width="100">
</tr>
<tr>
<td>ID</td>
<td>GUID</td>
<td>Name</td>
</tr>
<tr>
<td>1</td>
<td></td>
<td>thename</td>
</tr>
</table>
<p>but once the trigger is in place and you did the same process again of inserting a new name</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> testTable <span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;a new name&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<table>
<tr>
<td width="100">
<td width="100">
<td width="100">
</tr>
<tr>
<td>ID</td>
<td>GUID</td>
<td>Name</td>
</tr>
<tr>
<td>1</td>
<td></td>
<td>thename</td>
</tr>
<tr>
<td>1</td>
<td>3ace82c-2cf1-11df-b1c3-00a0d1a1240a</td>
<td>a new name</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/11/mysql-triggers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

