<?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>Hypn.za.net &#187; MySQL</title>
	<atom:link href="http://www.hypn.za.net/blog/category/programming/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hypn.za.net/blog</link>
	<description>Games, programming, and general geekyness =)</description>
	<lastBuildDate>Wed, 28 Jul 2010 21:28:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>MySQL auto-timestamping triggers</title>
		<link>http://www.hypn.za.net/blog/2008/09/22/mysql-auto-timestamping-triggers/</link>
		<comments>http://www.hypn.za.net/blog/2008/09/22/mysql-auto-timestamping-triggers/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 13:24:47 +0000</pubDate>
		<dc:creator>Hypn</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.hypn.za.net/blog/?p=144</guid>
		<description><![CDATA[I used to be dead against things like triggers&#8230; mainly because I hated coding them (and SQL) back when I learnt Oracle PL/SQL at college, and I&#8217;ve always been a &#8220;bare-minimum, must-be-backward-compatible, code-it-yourself&#8221; programmer&#8230; but now I&#8217;ve decided it&#8217;s just much easier to use them, and it&#8217;s not my fault if people refuse to upgrade [...]]]></description>
			<content:encoded><![CDATA[<p>I used to be dead against things like triggers&#8230; mainly because I hated coding them (and SQL) back when I learnt Oracle PL/SQL at college, and I&#8217;ve always been a &#8220;bare-minimum, must-be-backward-compatible, code-it-yourself&#8221; programmer&#8230; but now I&#8217;ve decided it&#8217;s just much easier to use them, and it&#8217;s not my fault if people refuse to upgrade to MySQL 5.</p>
<p>So here&#8217;s my niffty trigger code for the day, which automatically updates the &#8220;last_updated&#8221; (timestamp) field in a table, when a record is inserted or updated:</p>
<blockquote><p><em>CREATE TRIGGER `database_name`.`last_updated_insert_trigger`<br />
BEFORE INSERT ON `database_name`.`table_name` FOR EACH ROW SET new.last_updated := UNIX_TIMESTAMP();</em></p>
<p><em>CREATE TRIGGER `database_name`.`last_updated_update_trigger`<br />
BEFORE UPDATE ON `database_name`.`table_name` FOR EACH ROW SET new.last_updated := UNIX_TIMESTAMP();</em></p></blockquote>
<p>There&#8217;s probably a better way to do this (and I think you can combine both triggers, using &#8220;OR BEFORE UPDATE&#8221;?) but this is the first I&#8217;ve worked with triggers in MySQL and I&#8217;m too lazy to fiddle. Hope it helps. As I&#8217;m sure you can guess, `database_name` is your database name, `last_updated_update_trigger` is the name of your trigger, `table_name` is your table name, and `last_updated` is your field name that will contain the timestamp.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hypn.za.net/blog/2008/09/22/mysql-auto-timestamping-triggers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
