<?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>Frank&#039;s Java Development Blog &#187; Database</title>
	<atom:link href="http://javablog.franksalinas.net/category/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://javablog.franksalinas.net</link>
	<description>Java Enterprise Development &#38; Technology</description>
	<lastBuildDate>Sat, 26 Feb 2011 20:59:41 +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>Enforce Data Integrity With Check Constraints</title>
		<link>http://javablog.franksalinas.net/2008/11/23/enforce-data-integrity-with-check-constraints/</link>
		<comments>http://javablog.franksalinas.net/2008/11/23/enforce-data-integrity-with-check-constraints/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 17:25:39 +0000</pubDate>
		<dc:creator>Frank Salinas</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[Programming 101]]></category>

		<guid isPermaLink="false">http://javablog.franksalinas.net/?p=175</guid>
		<description><![CDATA[A short rant about using check constraints in your database.
No related posts.]]></description>
			<content:encoded><![CDATA[<p>I came across some code the other day that simply horrified me. I was looking at a table that had a field name which implied it stored a boolean value (e.g. IS_LAUGHABLE). The field type is defined as a char(1), allows NULL values and defined no default value. Looking at the data in the column I found a combination of the following values: T, F, Y, and N.<span id="more-175"></span></p>
<p>I didn&#8217;t know what to do. I wanted to insert a new row but could not decide which value is correct to denote for boolean true or false? Then I thought how would the code handle these values? The above value combinations make it impossible to use the ternary operator to retrieve the desired boolean value such as:</p>
<pre class="brush:java; smart-tabs:true; wrap-lines:false">
boolean isLaughable =
    resultSet.getString("is_laughable").equals("Y")?true:false);</pre>
<p>Then I found that someone had gone through the trouble of creating a utility method to convert the various character values to the desired boolean output of true or false. The method looked something like this:</p>
<pre class="brush:java; smart-tabs:true; wrap-lines:false" >static public boolean getBooleanFromString(String attr)
{
  if ( attr == null ) {
		return false ;
  }
  boolean val = false;

  if ("true".equalsIgnoreCase(attr)
         || "t".equalsIgnoreCase(attr)
         || "y".equalsIgnoreCase(attr)
         || "yes".equalsIgnoreCase(attr))
  {
		val = true;
  }

  return val;
}</pre>
<p>I couldn&#8217;t believe it! Instead of cleansing the data they just applied a Band-Aid to get around the issue. This is not the proper way to store this type of data. Possible values for this field can only be one of two values; true or false. Physically what characters represent those values is up to the developer. However since it represents a boolean value it should not allow more than two values to be inserted nor should it allow NULL values.</p>
<p>The proper action to take here is to clean up the data and add a check constraint to the table to restrict the data stored in this field. This can be done with two simple ALTER statements:</p>
<pre class="brush:sql; smart-tabs:true wrap-lines:false">ALTER TABLE jokes
ADD IS_LAUGHABLE char(1) DEFAULT 'N' NOT NULL; 

ALTER TABLE jokes
ADD CONSTRAINT jokes_is_laughable_ck
CHECK (IS_LAUGHABLE IN('Y','N'));</pre>
<p>Adding check constraints to your database will greatly increase data integrity, reduce data related bugs and make programming easier since you know exactly what values to expect from the database. Any situation where you have a finite list of values such as boolean values, or state (e.g. Error, Success, Processing, etc) are good candidates for value check constraints. Use good judgment and leverage check constraints to your advantage.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://javablog.franksalinas.net/2008/11/23/enforce-data-integrity-with-check-constraints/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: javablog.franksalinas.net @ 2012-02-07 02:42:09 -->
