<?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>Repeated Overflows &#187; SQL</title>
	<atom:link href="http://code.christophervigliotti.com/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.christophervigliotti.com</link>
	<description>Christopher Vigliotti often speaks in ColdFusion, Flash, SQL, CSS, Java and JavaScript.</description>
	<lastBuildDate>Thu, 02 Feb 2012 19:40:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Model-Glue, Oracle and YUI</title>
		<link>http://code.christophervigliotti.com/2009/11/model-glue-oracle-and-yui/</link>
		<comments>http://code.christophervigliotti.com/2009/11/model-glue-oracle-and-yui/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 15:45:21 +0000</pubDate>
		<dc:creator>Christopher Vigliotti</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Model-Glue]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://code.christophervigliotti.com/?p=370</guid>
		<description><![CDATA[I&#8217;ve been immersed in a world filled with Model-Glue Framework, Oracle 10g and YUI since June and am loving every minute of it. One of my goals for 2010 is to dust off this blog and resume contributing to the developer community. See you next year!]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been immersed in a world filled with <a href="http://docs.model-glue.com/">Model-Glue Framework</a>, <a href="http://www.oracle.com/technology/products/database/oracle10g/index.html">Oracle 10g</a> and <a href="http://developer.yahoo.com/yui/">YUI</a> since June and am loving every minute of it.  One of my goals for 2010 is to dust off this blog and resume contributing to the developer community.  See you next year!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.christophervigliotti.com/2009/11/model-glue-oracle-and-yui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing and Calling Stored Procedures</title>
		<link>http://code.christophervigliotti.com/2009/05/writing-and-calling-stored-procedures/</link>
		<comments>http://code.christophervigliotti.com/2009/05/writing-and-calling-stored-procedures/#comments</comments>
		<pubDate>Tue, 12 May 2009 17:42:57 +0000</pubDate>
		<dc:creator>Christopher Vigliotti</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://code.christophervigliotti.com/?p=136</guid>
		<description><![CDATA[In my previous post we built a Cheesy Database using simple SQL Scripts. Now I want to build on that by coding a stored procedure. Stored procedures are a ColdFusion developer&#8217;s best friend. You can use stored procedures to store some/most/all of your SQL logic. This is a great way to organize your code (stored [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://code.christophervigliotti.com/index.php/2009/05/creating-a-cheesy-database/">my previous post</a> we built a Cheesy Database using simple SQL Scripts.  Now I want to build on that by coding a stored procedure.  <a href="http://en.wikipedia.org/wiki/Stored_procedure">Stored procedures</a> are a ColdFusion developer&#8217;s best friend.  You can use stored procedures to store some/most/all of your SQL logic.  This is a great way to organize your code (stored procedures are generally faster than inline queries), optimize your application&#8217;s performance and help to protect your system from <a href="http://en.wikipedia.org/wiki/Sql_injection">SQL Injection</a>.</p>
<p>Writing a basic Stored Procedure is not a difficult task.  First lets take a look at our inline query&#8230;</p>
<p><span id="more-136"></span></p>
<p><em>Example 1</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfquery</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;addCheesesToFlavors&quot;</span> <span style="color: #0000FF;">datasource</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;CheesyDB&quot;</span><span style="color: #0000FF;">&gt;</span></span>
	INSERT INTO
		CheesesToFlavors
			(
			flavorId,
			cheeseId
			)
	VALUES
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfqueryparam</span> <span style="color: #0000FF;">value</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#variables.flavorId#&quot;</span> cfsqltype<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;cf_sql_int&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>,
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfqueryparam</span> <span style="color: #0000FF;">value</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#variables.cheeseId#&quot;</span> cfsqltype<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;cf_sql_int&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfquery</span><span style="color: #0000FF;">&gt;</span></span></pre></td></tr></table></div>

<p>In this simple INSERT statement we are inserting a new record into CheesesToFlavors.  Note the use of CFQUERYPARAM (a tag that all ColdFusion developers should be using).</p>
<p><strong>Writing A Stored Procedure</strong></p>
<p>Now lets take the above query and convert it to a stored procedure.  To add a new stored procedure browse to [your_database_engine] > CheesyDB > Programmability > Stored Procedures, then right click on &#8220;Stored Procedures&#8221; and select &#8220;New Stored Procedure&#8230;&#8221;.  Now you are ready to write some code!</p>
<p>First we declare our Stored Procedure and our input variables&#8230;</p>
<p><em>Example 2a</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span>  <span style="color: #993333; font-weight: bold;">PROCEDURE</span> uspAddCheesesToFlavors
	@cheeseId <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span>
	@flavorId <span style="color: #993333; font-weight: bold;">INT</span>
<span style="color: #993333; font-weight: bold;">AS</span></pre></td></tr></table></div>

<p>Line 1 declares that &#8220;this is a stored procedure named &#8216;uspAddCheesesToFlavors&#8217;&#8221;  Lines 2 and 3 are the two input variables that the procedure expects, and the datatype expected is int.  Lastly we have the AS keyword.</p>
<p>Next we insert the record into the database&#8230;</p>
<p><em>Example 2b</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span>
	CheesesToFlavors
		<span style="color: #66cc66;">&#40;</span>
		flavorId<span style="color: #66cc66;">,</span>
		cheeseId
		<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span>
	@flavorId<span style="color: #66cc66;">,</span>
	@cheeseId</pre></td></tr></table></div>

<p>This simple INSERT statement is similar to one that you would find in your inline queries.  On line 11 we&#8217;ve replaced the &#8220;VALUES&#8221; keyword found in our inline query with the &#8220;SELECT &#8221; keyword.   On lines 12 and 13 we output the same two variables that we declared at the top of the procedure.</p>
<p><strong>Calling Your Stored Procedure</strong></p>
<p><em>Example 3</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfstoredproc</span> procedure<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;uspAddCheesesToFlavors&quot;</span> <span style="color: #0000FF;">datasource</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;CheesyDB&quot;</span><span style="color: #0000FF;">&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfprocparam</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;in&quot;</span> cfsqltype<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;cf_sql_int&quot;</span> <span style="color: #0000FF;">value</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#variables.flavorId#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfprocparam</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;in&quot;</span> cfsqltype<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;cf_sql_int&quot;</span> <span style="color: #0000FF;">value</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#variables.cheeseId#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfstoredproc</span><span style="color: #0000FF;">&gt;</span></span></pre></td></tr></table></div>

<p>Yes it&#8217;s that easy.  Specify your procedure name and datasource name in the CFSTOREDPROC tag, declare your input variables in CFPROCPARAM tags and you are all set.  Note that your CFSTOREDPROC tags have to be in the same order as your variable declarations in order for your procedure to work as intended!</p>
<p><strong>Many Cheeses, Many Flavors</strong></p>
<p>Since we&#8217;re using our stored procedure to insert records into an intermediary table that joins Cheeses and Flavors we need to be mindful of duplicate records.  If we attempt to insert a duplicate record into this table we will throw an error.  We only have to add the code in example 2d to prevent our database from inserting duplicate records&#8230;</p>
<p><em>Example 2d</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span>
	<span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span>
		flavorId<span style="color: #66cc66;">,</span>
		cheeseId
	<span style="color: #993333; font-weight: bold;">FROM</span>
		CheesesToFlavors
	<span style="color: #993333; font-weight: bold;">WHERE</span>
		flavorId <span style="color: #66cc66;">=</span> @flavorId
		<span style="color: #993333; font-weight: bold;">AND</span> cheeseId <span style="color: #66cc66;">=</span> @cheeseId
	<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>For your cutting and pasting pleasure, here are all of the &#8220;Example 2&#8243; code samples merged into one place&#8230;</p>
<p><em>Example 4</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span>  <span style="color: #993333; font-weight: bold;">PROCEDURE</span> uspAddCheesesToFlavors
	@cheeseId <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span>
	@flavorId <span style="color: #993333; font-weight: bold;">INT</span>
<span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span>
	CheesesToFlavors
		<span style="color: #66cc66;">&#40;</span>
		flavorId<span style="color: #66cc66;">,</span>
		cheeseId
		<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span>
	@flavorId<span style="color: #66cc66;">,</span>
	@cheeseId
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span>
	<span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span>
		flavorId<span style="color: #66cc66;">,</span>
		cheeseId
	<span style="color: #993333; font-weight: bold;">FROM</span>
		CheesesToFlavors
	<span style="color: #993333; font-weight: bold;">WHERE</span>
		flavorId <span style="color: #66cc66;">=</span> @flavorId
		<span style="color: #993333; font-weight: bold;">AND</span> cheeseId <span style="color: #66cc66;">=</span> @cheeseId
	<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Booya!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.christophervigliotti.com/2009/05/writing-and-calling-stored-procedures/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating A Cheesy Database</title>
		<link>http://code.christophervigliotti.com/2009/05/creating-a-cheesy-database/</link>
		<comments>http://code.christophervigliotti.com/2009/05/creating-a-cheesy-database/#comments</comments>
		<pubDate>Tue, 12 May 2009 15:56:00 +0000</pubDate>
		<dc:creator>Christopher Vigliotti</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://code.christophervigliotti.com/?p=171</guid>
		<description><![CDATA[After reviewing my previous posts I thought that it would be helpful if all of my examples (1) worked and (2) worked together. So without further adieu, here are a few SQL scripts to get a basic database up and running. Once you&#8217;ve run these scripts you will be able to run the scripts found [...]]]></description>
			<content:encoded><![CDATA[<p>After reviewing my previous posts I thought that it would be helpful if all of my examples (1) worked and (2) worked together.   So without further adieu, here are a few SQL scripts to get a basic database up and running.  Once you&#8217;ve run these scripts you will be able to run the scripts found at <a href="http://code.christophervigliotti.com/index.php/2009/05/sql-joins/">my previous post on SQL Joins</a>.</p>
<p>First we&#8217;ll create the database&#8230;</p>
<p><span id="more-171"></span></p>
<p><em>Example 1a</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> CheesyDB</pre></td></tr></table></div>

<p>&#8230;then we&#8217;ll create the Odors table&#8230;</p>
<p><em>Example 1b</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Odors
	<span style="color: #66cc66;">&#40;</span>
	odorId <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	name <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>odorId<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>&#8230;lets also create a &#8220;Cheeses&#8221; table&#8230;</p>
<p><em>Example 1c</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Cheeses
	<span style="color: #66cc66;">&#40;</span>
	cheeseId <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	name <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	odorId <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>cheeseId<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">CONSTRAINT</span> fk_OdorsCheeses <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>odorId<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> Odors<span style="color: #66cc66;">&#40;</span>odorId<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>You should now have a new database named &#8220;CheesyDB&#8221; with two tables named &#8220;Cheeses&#8221; and &#8220;Odors&#8221;.  We should also have a foreign key constraint named fk_OdorsCheeses that establishes a one-to-many relationship between Cheeses and Odors.  Lets add a few more tables&#8230;</p>
<p><em>Example 1d</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Flavors
	<span style="color: #66cc66;">&#40;</span>
	flavorId <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	name <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>flavorId<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> CheesesToFlavors
<span style="color: #66cc66;">&#40;</span>
	cheeseId <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	flavorId <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
&nbsp;
	<span style="color: #993333; font-weight: bold;">CONSTRAINT</span> <span style="color: #66cc66;">&#91;</span>PK_CheeseToFlavor<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> CLUSTERED
	<span style="color: #66cc66;">&#40;</span>
		cheeseId<span style="color: #66cc66;">,</span>
		flavorId
	<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">CONSTRAINT</span> fk_Cheeses_CheesesToFlavors <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>cheeseId<span style="color: #66cc66;">&#41;</span>
		<span style="color: #993333; font-weight: bold;">REFERENCES</span> Cheeses<span style="color: #66cc66;">&#40;</span>cheeseId<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">CONSTRAINT</span> fk_Flavors_CheesesToFlavors <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>flavorId<span style="color: #66cc66;">&#41;</span>
		<span style="color: #993333; font-weight: bold;">REFERENCES</span> Flavors<span style="color: #66cc66;">&#40;</span>flavorId<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Now we have a new table &#8220;Flavors&#8221; and an intermediary table named &#8220;CheesesToFlavors&#8221; for tracking our many-to-many relationship between Cheeses and Flavors.     The code on lines 13-17 makes the data from both the cheeseId and flavorId columns the primary key, and the code on lines 18-21 defines the foreign key constraints of the table.</p>
<p>I&#8217;ll be referencing this database in a series of related SQL and ColdFusion posts over the coming weeks.  Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.christophervigliotti.com/2009/05/creating-a-cheesy-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Joins</title>
		<link>http://code.christophervigliotti.com/2009/05/sql-joins/</link>
		<comments>http://code.christophervigliotti.com/2009/05/sql-joins/#comments</comments>
		<pubDate>Fri, 08 May 2009 18:56:04 +0000</pubDate>
		<dc:creator>Christopher Vigliotti</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://code.christophervigliotti.com/?p=41</guid>
		<description><![CDATA[Having spent most of my career working in environments where SQL development and ColdFusion development are handled by separate people or teams. The end result is that my SQL skills are not as evolved as my ColdFusion skills. At my current job I am the one who writes the SQL for the small and medium-sized [...]]]></description>
			<content:encoded><![CDATA[<p>Having spent most of my career working in environments where SQL development and ColdFusion development are handled by separate people or teams.  The end result is that my SQL skills are not as evolved as my ColdFusion skills.</p>
<p>At my current job I am the one who writes the SQL for the small and medium-sized ColdFusion &#038; SQL Server-based systems that I develop. Up until the other day I used implicit inner joins and over-used subqueries to get the data that I wanted from a database. My code worked, but it was ugly.</p>
<p><span id="more-41"></span></p>
<p><strong>Ah Ha!</strong></p>
<p>My SQL deficiency became apparent as I was sharing some code with a comrade yesterday.  I was informed that my code had an &#8220;implicitly defined SQL Join&#8221;, and was informed that this style of coding is both deprecated and bad form.   I was writing my queries like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>	c<span style="color: #66cc66;">.</span>cheeseId<span style="color: #66cc66;">,</span>
	c<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span>
	o<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'odorName'</span>
<span style="color: #993333; font-weight: bold;">FROM</span>	Cheeses c<span style="color: #66cc66;">,</span> Odors o
<span style="color: #993333; font-weight: bold;">WHERE</span>	c<span style="color: #66cc66;">.</span>odorId <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>odorId</pre></div></div>

<p>&#8230;but as it turns out I should be writing them like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>	c<span style="color: #66cc66;">.</span>cheeseId<span style="color: #66cc66;">,</span>
	c<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span>
	o<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'odorName'</span>
<span style="color: #993333; font-weight: bold;">FROM</span>	Cheeses c
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span>
	Odors o <span style="color: #993333; font-weight: bold;">ON</span> c<span style="color: #66cc66;">.</span>odorId <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>odorId</pre></div></div>

<p>Both queries will return the same results, but the latter is well formed, and consistent with other join types that you may have in your application.</p>
<p>Once I understood that, a lightbulb appeared over my head.  I kicked out a few more code samples to observe the results of left and right joins (a concept that I have been having problems grasping).</p>
<p><strong>Left Joins In The House</strong></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>	c<span style="color: #66cc66;">.</span>cheeseId<span style="color: #66cc66;">,</span>
	c<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span>
	o<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'odorName'</span>
<span style="color: #993333; font-weight: bold;">FROM</span>	Cheeses c
<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span>
	Odors o <span style="color: #993333; font-weight: bold;">ON</span> c<span style="color: #66cc66;">.</span>odorId <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>odorId</pre></div></div>

<p>The above SQL will return all cheeses and their associated odors.  Cheeses that don&#8217;t have an odor will also be returned with a null value in the odorName column.</p>
<p><strong>Right Joins In The House</strong></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>	c<span style="color: #66cc66;">.</span>cheeseId<span style="color: #66cc66;">,</span>
	c<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span>
	o<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'odorName'</span>
<span style="color: #993333; font-weight: bold;">FROM</span>	Cheeses c
<span style="color: #993333; font-weight: bold;">RIGHT</span> <span style="color: #993333; font-weight: bold;">JOIN</span>
	Odors o <span style="color: #993333; font-weight: bold;">ON</span> c<span style="color: #66cc66;">.</span>odorId <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>odorId</pre></div></div>

<p>The above SQL will return all odors and the cheeses that they relate to.  Odors that don&#8217;t link to an associated cheese will also be returned with a null value in the cheeseId and name columns.</p>
<p>It&#8217;s interesting how not understanding one bit of theory or syntax can act as a closed door in your mind that keeps you from understanding a whole world of knowledge.  This is why I always maintain an open mind and take a bit of time each day to read other developer&#8217;s blogs.</p>
<style type="text/css">
table.sqlTable{
	width:200px;
	border:1px solid silver;
}
table.sqlTable td{
	padding:2px;
	border-bottom:1px solid silver;
	font-size:12px;
}
</style>
]]></content:encoded>
			<wfw:commentRss>http://code.christophervigliotti.com/2009/05/sql-joins/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

