<?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>David McMurray Dot Net&#187; SQL</title>
	<atom:link href="http://www.davidmcmurray.net/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davidmcmurray.net</link>
	<description>Live To Code, Code To Live</description>
	<lastBuildDate>Wed, 18 Aug 2010 19:24:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Trigger Happy with MySQL</title>
		<link>http://www.davidmcmurray.net/2009/05/15/trigger-happy-with-mysql/</link>
		<comments>http://www.davidmcmurray.net/2009/05/15/trigger-happy-with-mysql/#comments</comments>
		<pubDate>Fri, 15 May 2009 01:16:21 +0000</pubDate>
		<dc:creator>David McMurray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[triggers]]></category>

		<guid isPermaLink="false">http://www.davidmcmurray.net/?p=211</guid>
		<description><![CDATA[I have to say I am a rather big fan of db triggers, in an effort to bring some discipline to my databases. Since audit tables have become quite a habit of mine, this is probably my main use for triggers. Although MySQL has a little way to go yet before we have the kind [...]]]></description>
			<content:encoded><![CDATA[<p>I have to say I am a rather big fan of db triggers, in an effort to bring some discipline to my databases. Since audit tables have become quite a habit of mine, this is probably my main use for triggers. Although MySQL has a little way to go yet before we have the kind of experience that SQL Server has brought to us in the past, but it&#8217;s getting there.</p>
<p>I was surprised to see how little documentation there is on the &#8216;BEFORE&#8217; and &#8216;AFTER&#8217; keywords. I was looking for something that would explain to me when I should use each of these, and wherever I found *something* on it, it wasn&#8217;t enough.</p>
<p>Through a little bit of testing however, I have found that using &#8216;AFTER&#8217; delivers desirable results for Inserts, Updates and Deletes. Is it important? Well, considering that after performing the tasks of a &#8216;BEFORE&#8217; trigger the actual Insert, Update or Delete might fail, but this doesn&#8217;t undo the effects of the trigger. Using &#8216;AFTER&#8217; triggers ensures that the task has completed before we create the audit record.</p>
<p>An example of auditing triggers, for Insert, Update and Delete:</p>
<p>
<pre><code>
CREATE TRIGGER mytable_audit_trigger_insert
AFTER INSERT ON mytable
FOR EACH ROW BEGIN
    INSERT INTO mytable_audit (
        id, date, name
    )
    VALUES(
        NEW.id, NOW(), NEW.name
    );
END;
</code></pre>
</p>
<p>
<pre><code>
CREATE TRIGGER mytable_audit_trigger_update
AFTER UPDATE ON mytable
FOR EACH ROW BEGIN
    INSERT INTO mytable_audit (
        id, date, name
    )
    VALUES(
        NEW.id, NOW(), NEW.name
    );
END;
</code></pre>
</p>
<p>
<pre><code>
CREATE TRIGGER mytable_audit_trigger_delete
AFTER DELETE ON mytable
FOR EACH ROW BEGIN
    INSERT INTO mytable_audit (
        id, date, name
    )
    VALUES(
        OLD.id, NOW(), OLD.name
    );
END;
</code></pre>
</p>
<p>It would be nice to be able to query/edit triggers, but unfortunately you can only CREATE and DROP them, certainly within the MySQL Query Browser. I&#8217;ve not tried any 3rd party tools as I&#8217;m happy to keep .sql files, with the details of the CREATE TRIGGER statements so I know what&#8217;s in them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidmcmurray.net/2009/05/15/trigger-happy-with-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

