<?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>isageo &#187; mysql</title>
	<atom:link href="http://www.isageo.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.isageo.com</link>
	<description>about all aspects of life</description>
	<lastBuildDate>Thu, 19 Jan 2012 12:14:36 +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>Data Versioning using Triggers in MySQL</title>
		<link>http://www.isageo.com/2010/02/data-versioning-using-triggers-in-mysql/</link>
		<comments>http://www.isageo.com/2010/02/data-versioning-using-triggers-in-mysql/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 16:01:48 +0000</pubDate>
		<dc:creator>geo</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[triggers]]></category>
		<category><![CDATA[versioning]]></category>

		<guid isPermaLink="false">http://www.isageo.com/?p=189</guid>
		<description><![CDATA[At any time during database driven development you ask yourself how to keep track of the changes made to your data in your database. I asked myself the same question. And I came up with a simple solution using triggers. A trigger is a small function stored inside your database that gets triggered on a [...]]]></description>
			<content:encoded><![CDATA[<p>At any time during database driven development you ask yourself how to keep track of the changes made to your data in your database. I asked myself the same question. And I came up with a simple solution using triggers.</p>
<p>A trigger is a small function stored inside your database that gets triggered on a event in your database. Like, the moment you add or update a row in your table.</p>
<p>For versioning we need 2 things:</p>
<p>- a few extra fields in your table<br />
- a copy of the table structure of the table you want versioning turned on</p>
<p>First, let&#8217;s define 2 extra fields:</p>
<p>- <strong>enuVersionStatus</strong> (ENUM) = &#8216;INSERT&#8217;, &#8216;UPDATE&#8217;, &#8216;DELETE&#8217;<br />
- <strong>tsVersionStatus</strong> (timestamp) = current_timestamp &amp; on update current_timestamp</p>
<p><strong>enuVersionStatus</strong> is defined as a enumerable field that holds the state of the data<br />
<strong>tsVersionStatus</strong> is defined as a timestamp and holds the timestamp when the data has been edited</p>
<p>Now we copy the entire structure of our table to a log, or history table. Let&#8217;s just append a &#8220;_log&#8221; to this table and remove any indexes or auto_increment statements. And lastly we create a new trigger:</p>
<blockquote><p>DELIMITER $$<br />
CREATE TRIGGER myTable_Trigger<br />
BEFORE UPDATE ON myTable<br />
FOR EACH ROW<br />
BEGIN<br />
INSERT INTO myTable_log (SELECT * FROM myTable WHERE id = OLD.id);<br />
END$$</p></blockquote>
<p>What the trigger does is very simple. Just before doing any update on the table, the &#8220;old&#8221; entry is being written into the log table. The data in myTable holds the live data and the myTable_log holds all the old entries.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.isageo.com/2010/02/data-versioning-using-triggers-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

