<?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>Code With Design &#187; SQL</title>
	<atom:link href="http://codewithdesign.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://codewithdesign.com</link>
	<description>web application development blog by Caleb Jonasson</description>
	<lastBuildDate>Thu, 15 Sep 2011 17:52:47 +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>SQL: Using Unions in Queries</title>
		<link>http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/</link>
		<comments>http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 12:54:58 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[union]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=503</guid>
		<description><![CDATA[TweetThe union is a very nice feature to have when working with pulling multiple items from multiple tables. This article is going to go over how to use a union to join information under designated columns. What is a Union A union is a feature of sql that allows us to join information from multiple [...]


Related posts:<ol><li><a href='http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/' rel='bookmark' title='Permanent Link: SQL: One To Many Using Inner Join'>SQL: One To Many Using Inner Join</a></li>
<li><a href='http://codewithdesign.com/2010/05/23/sql-%e2%80%93-an-introduction-to-keys/' rel='bookmark' title='Permanent Link: SQL – An introduction to Keys'>SQL – An introduction to Keys</a></li>
<li><a href='http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/' rel='bookmark' title='Permanent Link: Getting Secured Using Mysqli'>Getting Secured Using Mysqli</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="SQL: Using Unions in Queries" data-via="" data-url="http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><g:plusone size="tall" href="http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/"></g:plusone><p>The union is a very nice feature to have when working with pulling multiple items from multiple tables. This article is going to go over how to use a union to join information under designated columns.</p>
<h2>What is a Union</h2>
<p>A union is a feature of sql that allows us to join information from multiple tables and store it into one singular table. There are a few requirements in order to do this properly and if the requirements are not followed you are going to get a few error messages and/or incorrect data returned.</p>
<p>Rules of the Union Operator<br />
<strong>1. </strong><em>The union operator requires the same amount of columns.</em><br />
<strong>2.</strong> <em>All of the columns need to have the same name.</em><br />
<strong>3.</strong> <em>The union operator needs to be placed between queries.</em></p>
<h2>Column Setup</h2>
<p>Because you need to have the same amount of columns and the same names for each columns there is a high chance that rather than parsing the content you will just be displaying generic information such as type, title and content.<br />
<em>Note: There is <strong>no limit </strong>to the amount of queries you are able to union together but it would end up being a rather large query.</em></p>
<h2>An example of a working union operator.</h2>

<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="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> name<span style="color: #66cc66;">,</span> job<span style="color: #66cc66;">,</span> title<span style="color: #66cc66;">,</span> salary
<span style="color: #993333; font-weight: bold;">FROM</span> job_1_employees
<span style="color: #993333; font-weight: bold;">WHERE</span> job
<span style="color: #993333; font-weight: bold;">LIKE</span> ?
&nbsp;
<span style="color: #993333; font-weight: bold;">UNION</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> name<span style="color: #66cc66;">,</span> job<span style="color: #66cc66;">,</span> title<span style="color: #66cc66;">,</span> salary
<span style="color: #993333; font-weight: bold;">FROM</span> job_2_employees
<span style="color: #993333; font-weight: bold;">WHERE</span> job
<span style="color: #993333; font-weight: bold;">LIKE</span> ?</pre></td></tr></table></div>

<p>In this query we are pulling the information from both tables and merging the content into one large list of company and sub company employees. The search is done based on the like operator.</p>
<h2>An example of a broken union operator</h2>

<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="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> book_nid<span style="color: #66cc66;">,</span> author<span style="color: #66cc66;">,</span> title<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">YEAR</span>
<span style="color: #993333; font-weight: bold;">FROM</span> book_listings
<span style="color: #993333; font-weight: bold;">WHERE</span> title
<span style="color: #993333; font-weight: bold;">LIKE</span> ?
&nbsp;
<span style="color: #993333; font-weight: bold;">UNION</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> magazine_nid<span style="color: #66cc66;">,</span> publisher<span style="color: #66cc66;">,</span> title<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">YEAR</span>
<span style="color: #993333; font-weight: bold;">FROM</span> magazine_listings
<span style="color: #993333; font-weight: bold;">WHERE</span> title
<span style="color: #993333; font-weight: bold;">LIKE</span> ?</pre></td></tr></table></div>

<p>In order to get a query like this to work properly we are going to have to store the information into some renamed columns to get it to work. Whenever dealing with a work around it is best to create an extra column with a null value rather than changing up the entire query format.</p>
<h2>How to Fix the Union Query</h2>

<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="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> book_nid nid<span style="color: #66cc66;">,</span> author creator<span style="color: #66cc66;">,</span> title<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'book'</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TYPE</span>
<span style="color: #993333; font-weight: bold;">FROM</span> book_listings
<span style="color: #993333; font-weight: bold;">WHERE</span> title
<span style="color: #993333; font-weight: bold;">LIKE</span> ?
&nbsp;
<span style="color: #993333; font-weight: bold;">UNION</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> magazine_nid nid<span style="color: #66cc66;">,</span> publisher creator<span style="color: #66cc66;">,</span> title<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'magazine'</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TYPE</span>
<span style="color: #993333; font-weight: bold;">FROM</span> magazine_listings
<span style="color: #993333; font-weight: bold;">WHERE</span> title
<span style="color: #993333; font-weight: bold;">LIKE</span> ?</pre></td></tr></table></div>

<p>You will notice that we have changed some of the different columns and created another column that will store the type of field that is being returned. This is a good thing to have when you need to create a link to a specific kind of page or display a special icon.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=503&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/' rel='bookmark' title='Permanent Link: SQL: One To Many Using Inner Join'>SQL: One To Many Using Inner Join</a></li>
<li><a href='http://codewithdesign.com/2010/05/23/sql-%e2%80%93-an-introduction-to-keys/' rel='bookmark' title='Permanent Link: SQL – An introduction to Keys'>SQL – An introduction to Keys</a></li>
<li><a href='http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/' rel='bookmark' title='Permanent Link: Getting Secured Using Mysqli'>Getting Secured Using Mysqli</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Searching With %?% In a Prepared Statement</title>
		<link>http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/</link>
		<comments>http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 10:17:43 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[%?%]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[wildcard]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=491</guid>
		<description><![CDATA[TweetA while back when working on a project I learned that when using the wild card: &#8216;%&#8217; in SQL with a prepared statement you are going to get an error message. This is because the prepared statement class has an issue with a wild card surrounding wild cards. &#8216;%?%&#8217;. Because of this a simple sql [...]


Related posts:<ol><li><a href='http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/' rel='bookmark' title='Permanent Link: Getting Secured Using Mysqli'>Getting Secured Using Mysqli</a></li>
<li><a href='http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/' rel='bookmark' title='Permanent Link: SQL: One To Many Using Inner Join'>SQL: One To Many Using Inner Join</a></li>
<li><a href='http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/' rel='bookmark' title='Permanent Link: SQL: Using Unions in Queries'>SQL: Using Unions in Queries</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Searching With %?% In a Prepared Statement" data-via="" data-url="http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><g:plusone size="tall" href="http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/"></g:plusone><p>A while back when working on a project I learned that when using the wild card: &#8216;%&#8217; in SQL with a prepared statement you are going to get an error message. This is because the prepared statement class has an issue with a wild card surrounding wild cards. &#8216;%?%&#8217;.</p>
<p>Because of this a simple sql query such as this is going to have issues pulling data from the database.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM tbl_comments WHERE comment LIKE %?% LIMIT 20&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Rather than attempt to get the above code to work you are going to need to take a separate approach and store the wild cards in with the search variable. This only takes one extra line of code and your search will be back up and running in no time at all.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$comment</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'%'</span><span style="color: #339933;">.</span><span style="color: #000088;">$comment</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'%'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM tbl_comments WHERE comment LIKE ? LIMIT 20&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<img src="http://codewithdesign.com/?ak_action=api_record_view&id=491&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/' rel='bookmark' title='Permanent Link: Getting Secured Using Mysqli'>Getting Secured Using Mysqli</a></li>
<li><a href='http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/' rel='bookmark' title='Permanent Link: SQL: One To Many Using Inner Join'>SQL: One To Many Using Inner Join</a></li>
<li><a href='http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/' rel='bookmark' title='Permanent Link: SQL: Using Unions in Queries'>SQL: Using Unions in Queries</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL – An introduction to Keys</title>
		<link>http://codewithdesign.com/2010/05/23/sql-%e2%80%93-an-introduction-to-keys/</link>
		<comments>http://codewithdesign.com/2010/05/23/sql-%e2%80%93-an-introduction-to-keys/#comments</comments>
		<pubDate>Mon, 24 May 2010 01:23:02 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[foreign key]]></category>
		<category><![CDATA[Primary key]]></category>
		<category><![CDATA[unique key]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=409</guid>
		<description><![CDATA[This is a tutorial on the basic uses of keys and how they work.


Related posts:<ol><li><a href='http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/' rel='bookmark' title='Permanent Link: SQL: One To Many Using Inner Join'>SQL: One To Many Using Inner Join</a></li>
<li><a href='http://codewithdesign.com/2011/07/02/password-recovery-theory/' rel='bookmark' title='Permanent Link: Password Recovery {Theory}'>Password Recovery {Theory}</a></li>
<li><a href='http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/' rel='bookmark' title='Permanent Link: SQL: Using Unions in Queries'>SQL: Using Unions in Queries</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="SQL – An introduction to Keys" data-via="" data-url="http://codewithdesign.com/2010/05/23/sql-%e2%80%93-an-introduction-to-keys/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><g:plusone size="tall" href="http://codewithdesign.com/2010/05/23/sql-%e2%80%93-an-introduction-to-keys/"></g:plusone><p><strong>The Primary Key</strong></p>
<p>Cells inside of your database are all located through an X-axis and a Y-axis. The X-axis is a row and the Y-axis is the column. Because the information inside of a database is stored downward as you go through the table each added record needs a unique identifier to be found. This is where the primary key comes in.</p>
<p>For every table in your database a unique ID is required to find the records and because of this every table has exactly one Primary Key. Without a primary key there would be no way to reference the information stored inside of that row and data would become inaccessible very quickly. This is also the reason why the primary key cannot have the value of “null.”</p>
<p>In a table there can never two of the same key. Two keys would conflict with each other and return bad records, which is why it is impossible to have this. Generally when working on a database you never want to modify the key after it has been created. This would change how the data is accessed and would be a bad idea when it comes to referencing the information should anything go wrong. There are two different kinds of keys. The first kind is a simple key and which is made of one column. The second kind of key is a composite key, which is made of 2 or more columns.</p>
<p>In most cases the Primary key is an integer. This allows simple ordering and counting of records on system however there are cases where a hex number can be used or even a pattern of numbers and letters.</p>
<p>For example, say that we were creating a database for items in a store. Each item has a unique SKU but this is something can change over time so we would still need our primary key to be a number that is completely unique. The naming also will not work for a unique ID due to name changes or the same product name with a change in SKU, which happens over time with different versions and packaging.</p>
<p><strong>Foreign Keys</strong></p>
<p>Foreign keys are used to reference objects or rows from other tables. Theses keys generally reference parent tables from child tables and are used to cross data between tables. This means that the table with the foreign key is always going to be the child in the link.</p>
<p>Unlike the primary key, the foreign key can have a null value, which leads to no link being made between the two tables for that object. These keys also do not need to be unique; in fact they often never are unique do to the nature of how they are used.</p>
<p>An example of the foreign key, continuing from the example used in primary keys, will require us to add another column to the initial table so we will now have ID, SKU, and Vendor. The tables will look like this.</p>
<p><a href="http://codewithdesign.com/wp-content/uploads/2010/05/Screen-shot-2010-05-23-at-6.14.41-PM.png" rel="lightbox[409]"><img class="alignnone size-medium wp-image-410" title="Foreign and unique keys in sql" src="http://codewithdesign.com/wp-content/uploads/2010/05/Screen-shot-2010-05-23-at-6.14.41-PM-600x266.png" alt="" width="360" height="160" /></a></p>
<p>As you can see, in this example we have the foreign key doubling as a primary key in the lower table, which is used in as a vendor in the top table. This is a quick and simple way to store the values easily in the database and comes in handy when working with relationships.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=409&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/' rel='bookmark' title='Permanent Link: SQL: One To Many Using Inner Join'>SQL: One To Many Using Inner Join</a></li>
<li><a href='http://codewithdesign.com/2011/07/02/password-recovery-theory/' rel='bookmark' title='Permanent Link: Password Recovery {Theory}'>Password Recovery {Theory}</a></li>
<li><a href='http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/' rel='bookmark' title='Permanent Link: SQL: Using Unions in Queries'>SQL: Using Unions in Queries</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2010/05/23/sql-%e2%80%93-an-introduction-to-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

