<?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>Jason Fox &#187; databases</title>
	<atom:link href="http://www.jasonfox.com/tag/databases/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jasonfox.com</link>
	<description>programming, products, and pontifications...</description>
	<lastBuildDate>Thu, 17 Jun 2010 02:29:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL Allows NULLs Where They Are Not Welcome</title>
		<link>http://www.jasonfox.com/2009/02/mysql-allows-nulls-where-they-are-not-allowed/</link>
		<comments>http://www.jasonfox.com/2009/02/mysql-allows-nulls-where-they-are-not-allowed/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 18:18:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.jasonfox.com/?p=94</guid>
		<description><![CDATA[I recently came across an annoying bug in MySQL v5.1 (also in 6.0 apparently) that bit me hard, so, I thought I&#8217;d post on it in case you are being bitten by the same bug.
If you attempt to update a column that does not allow NULL to NULL, MySQL will set the column&#8217;s value to [...]]]></description>
			<content:encoded><![CDATA[<p>I recently came across an annoying bug in MySQL v5.1 (also in 6.0 apparently) that bit me hard, so, I thought I&#8217;d post on it in case you are being bitten by the same bug.</p>
<p>If you attempt to update a column that does not allow NULL to NULL, MySQL will set the column&#8217;s value to the default value for that column&#8217;s data type.  This is true only when you are not running MySQL <a href="http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_strict_trans_tables">strict mode</a>.  Here&#8217;s an example to illustrate.</p>
<pre>mysql&gt; create table null_test (id int not null unique(id), name varchar(25)
null default null);
Query OK, 0 rows affected (0.01 sec)

mysql&gt; show create table null_test;
+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table     | Create Table                                                                                                                                           |
+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| null_test | CREATE TABLE `null_test` (
  `id` int(11) NOT NULL,
  `name` varchar(25) default NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql&gt; insert into null_test (id, name) values (1, 'Jane');
Query OK, 1 row affected (0.00 sec)

mysql&gt; update null_test set id = null;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql&gt; select * from null_test where name = 'Jane';
+----+------+
| id | name |
+----+------+
|  0 | Jane |
+----+------+
1 row in set (0.00 sec)</pre>
<p>More information about this bug can be found in the <a href="http://bugs.mysql.com/bug.php?id=33699">bug report</a> submitted Janurary 4, 2008.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonfox.com/2009/02/mysql-allows-nulls-where-they-are-not-allowed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Check Constraints and MySQL</title>
		<link>http://www.jasonfox.com/2009/02/check-constraints-and-mysql/</link>
		<comments>http://www.jasonfox.com/2009/02/check-constraints-and-mysql/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 00:47:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rubyonrails]]></category>

		<guid isPermaLink="false">http://www.jasonfox.com/?p=80</guid>
		<description><![CDATA[Unfortunately, MySQL does not support check constraints out of the box.  This makes the task of enforcing business logic in the database layer difficult, but not impossible.  ]]></description>
			<content:encoded><![CDATA[<div class="discussion">
<p class="description">Unfortunately, MySQL does not support <a href="http://en.wikipedia.org/wiki/Check_Constraint">check constraints</a> out of the box.  This makes the task of enforcing business logic in the database layer difficult, but not impossible.  I recently found <a href="http://forge.mysql.com/wiki/Triggers#Emulating_Check_Constraints">this approach</a> to implementing check constraints in MySQL.  It&#8217;s not as pretty and clean as I&#8217;d like, but, it&#8217;s the best approach that I&#8217;ve found so far.</p>
<p>Now. why would you want to encode business logic in the database?  Can&#8217;t you make due with your <a href="http://api.rubyonrails.com/classes/ActiveRecord/Validations.html">ActiveRecord::Validations</a>?</p>
<p>Well, have you ever updated the database directly?  Have you ever called update_attribute on an object?  How about save_with_validation(false)?   Yeah, I thought so.  Read more about why you should treat your database as a fortress in <a href="http://blog.chak.org/">Dan Chak</a>&#8217;s recently released book, <a href="http://www.amazon.com/Enterprise-Rails-Dan-Chak/dp/0596515200/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1233881182&amp;sr=8-1">Enterprise Rails</a> (review coming soon).</div>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonfox.com/2009/02/check-constraints-and-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rewriting Sub-selects as Joins</title>
		<link>http://www.jasonfox.com/2009/02/rewriting-sub-selects-as-joins/</link>
		<comments>http://www.jasonfox.com/2009/02/rewriting-sub-selects-as-joins/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 23:07:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.jasonfox.com/?p=68</guid>
		<description><![CDATA[Your RDBMS will usually rewrite your sub-selects behind the scenes as joins.  However, there are times where you'll want to do this yourself. Here are a couple examples of how to rewrite sub-selects as joins.]]></description>
			<content:encoded><![CDATA[<p>Your RDBMS will usually rewrite your sub-selects behind the scenes as joins.  However, there are times where you&#8217;ll want to do this yourself.  For example, past versions of MySQL did not play well with sub-selects.  Here are a couple examples of how to rewrite sub-selects as joins.</p>
<pre>select *
from   table_a
where  id not in (select a_id from table_b);
<strong>-- can be rewritten as...
</strong>select *
from   table_a
left   outer join table_b on table_a.id = table_b.a_id
where  table_b.a_id is null;

select *
from   table_a
where  id in (select a_id from table_b);
<strong>-- can be rewritten as...</strong>
select *
from   table_a
left outer join table_b on table_a.id = table_b.a_id
where  table_b.a_id is not null;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonfox.com/2009/02/rewriting-sub-selects-as-joins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
