<?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>Sean&#039;s Blog &#187; triggers</title>
	<atom:link href="http://seanbehan.com/tag/triggers/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanbehan.com</link>
	<description>Web Programming, Ruby on Rails, Wordpress, PHP from Burlington, Vermont</description>
	<lastBuildDate>Wed, 08 Sep 2010 16:10:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Managing Timestamps in MySQL with a Trigger</title>
		<link>http://seanbehan.com/databases/managing-timestamps-in-mysql-with-a-trigger/</link>
		<comments>http://seanbehan.com/databases/managing-timestamps-in-mysql-with-a-trigger/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 17:13:51 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[timestamping]]></category>
		<category><![CDATA[triggers]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=707</guid>
		<description><![CDATA[MySQL doesn&#8217;t support having two columns with time stamping on both initialization and/or on updating at the same time. It would be nice to be able to do *this* where the created_at column gets the current_timestamp on initialization and the updated_at gets changed on updating the row. # like so doesn't work... create table entries( [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL doesn&#8217;t support having two columns with time stamping on both initialization and/or on updating at the same time. It would be nice to be able to do *this* where the created_at column gets the current_timestamp on initialization and the updated_at gets changed on updating the row.</p>
<pre>
# like so doesn't work...
create table entries(
  body blob,
  created_at datetime default current_timestamp,
  updated_at timestamp default current_timestamp on update current_timestamp
);
</pre>
<p>Seems like a feature a lot of folks would like. There are two work-arounds. The first is baking it into your application code with something like</p>
<pre>
create table entries(
  body blob,
  created_at datetime default null,
  updated_at timestamp default current_timestamp on update current_timestamp
);
insert into entries (body, created_at) values ('hello world', now());
</pre>
<p>The second way is to create a trigger and call the trigger on your insert action on a row.</p>
<pre>
create table entries (
  body  blob,
  created_at datetime default null,
  updated_at timestamp default null on update current_timestamp
);
create trigger init_created_at before insert on entries for each row set new.created_at = now();
</pre>
<p>Now whenever a new row is created the trigger will be executed and set the time to the current timestamp. You can forget about the created_at column in your code because it&#8217;s not meant to be changed.</p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/databases/managing-timestamps-in-mysql-with-a-trigger/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
