<?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>Geekgirl&#039;s Plain English Computing &#187; rosevines</title>
	<atom:link href="http://www.geekgirls.com/author/rosevines/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.geekgirls.com</link>
	<description>Tutorials and commentary on all things Windows &#38; web, with a few tangential offerings.</description>
	<lastBuildDate>Mon, 31 Oct 2011 12:17:56 +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>Databasics VII: Parameter Queries</title>
		<link>http://www.geekgirls.com/2011/09/databasics-vii-parameter-queries/</link>
		<comments>http://www.geekgirls.com/2011/09/databasics-vii-parameter-queries/#comments</comments>
		<pubDate>Thu, 29 Sep 2011 23:49:28 +0000</pubDate>
		<dc:creator>rosevines</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Office Apps]]></category>
		<category><![CDATA[database design]]></category>
		<category><![CDATA[database queries]]></category>
		<category><![CDATA[dbms]]></category>
		<category><![CDATA[microsoft access]]></category>
		<category><![CDATA[queries]]></category>

		<guid isPermaLink="false">http://www.geekgirls.com/?p=1399</guid>
		<description><![CDATA[Interactive, on-the-fly parameter queries add a huge amount of flexibility and power to run-of-the-mill static queries. Learn how to create your own using Microsoft Access.]]></description>
			<content:encoded><![CDATA[<div class="woo-sc-box info   full">Note: I&#8217;m working on updating this series of articles. This article is really showing its age. All the examples are for  Microsoft Access 2000/2003, and things have changed a lot since then. While the concepts are still the same and you can learn a lot from those, the step-by-steps are quite different if you&#8217;re working with a newer database program such as Access 2007 or 2010. I plan to produce a new article with Access 2010 examples in the next month or so, so if you&#8217;re interested, check back in a while.</div>
<p><span class="dropcap">I</span><!--/.dropcap-->f the title of this tutorial sounds a little intimidating, never fear. Toss the term &#8216;parameter query&#8217; around at a party and you may get a few gasps of admiration, but understanding and using this highly useful type of query is no big deal.</p>
<p>A parameter query is interactive – it prompts the user for information when it&#8217;s run. You can use a parameter query to ask the user for criteria for retrieving records or for a particular value to insert in a field. This makes parameter queries great for generating dynamic, on-the-fly results.</p>
<p>If you think of the queries we&#8217;ve examined in earlier articles in this series, while the data may change, the query itself is static:</p>
<ul>
<li>Which countries have a population over 20 million and an area of over 2 million square kilometres?</li>
<li>Which country has the smallest population density?</li>
</ul>
<p>With parameter queries, both the data and the queries may change over time:</p>
<ul>
<li>Which countries have a population between x and y?</li>
<li>Which countries joined the United Nations in the nth decade of 2000?</li>
</ul>
<p>You obtain the values for those variables – x, y and n – by asking the user to supply them each time the query is run.</p>
<h2>Creating a parameter query</h2>
<p>Let&#8217;s take a look at some parameter queries in action, using Microsoft Access, which provides tools that let us do this job easily. We&#8217;ll use the Countries database we&#8217;ve used previously (you&#8217;ll find a download link at the bottom of this page) and start by creating a non-parameter query to answer the question &#8220;Which countries have a population greater than one hundred million?&#8221; We&#8217;ll then transform that query into a parameter query and contrast the results.</p>
<p>Here goes:</p>
<ol>
<li>Open the Countries database in Access.</li>
<li>In the main Database Window, click Queries in the Objects list.</li>
<li>Double-click Create Query In Design View to open the Query window and Show Table box.</li>
<li>Select the Countries table to add it to the Query window, click Add, and then click Close.</li>
<li>Click Name in the list of fields, hold down the Ctrl key and click Population. Drag the two fields to the left-hand column in the query design grid to add the fields to the query.</li>
<li>In the Population column in the grid, type:</li>
</ol>
<blockquote><p><strong>&gt;100000000</strong></p>
<p>in the Criteria row.</p></blockquote>
<ol start="7">
<li>Click the Run button (it has an exclamation mark on it) to run the query. You should see 11 countries listed, each with a population greater than 100 million.</li>
</ol>
<p>That&#8217;s our simple, non-parameter query, which we created by providing a constant (100 million) as the matching criteria. Now let&#8217;s change it into a more flexible parameter query, which uses variables provided by the user instead of a constant.</p>
<ol>
<li>Click the View button (it&#8217;s the left-most button on the toolbar) to return to Design View, so we can modify the query.</li>
<li>Delete the contents of the Criteria cell in the Population column and replace it with:</li>
</ol>
<blockquote><p><strong>&gt;[Show countries with a population over:]</strong></p>
<p>You may need to resize the column (by dragging the right edge of the column header to the right) so you can see the entire contents.</p></blockquote>
<ol start="3">
<li>Click the Run button. You&#8217;ll be presented with a dialog box containing the prompt, &#8220;Show countries with a population over:&#8221; Fill in any value you like and click OK to see the results.</li>
<li>Save this query by clicking the Save button and naming it <em>Population over x</em>.</li>
</ol>
<h2>Parameter tips</h2>
<p>Neat, huh? Let&#8217;s examine the query to see how we did this.</p>
<p>The criteria consists of the greater than operator (&gt;) followed by the prompt we wish to use enclosed in square brackets:</p>
<p><strong>&gt;[Show countries with a population over:]</strong></p>
<p>That&#8217;s simple enough, but there are two important things to keep in mind when you create your parameter prompts. First, be succinct. Access lops the prompt off at 37 characters, so if you are verbose you&#8217;ll end up with shabby-looking, truncated prompts.</p>
<p>Second, be as clear as possible. Although you want to be succinct, don&#8217;t become cryptic. For instance, you could just as easily use the criteria:</p>
<p><strong>&gt;[Pop greater than:]</strong></p>
<p>or even</p>
<p><strong>&gt;[Pop?]</strong></p>
<p>Those will work, but if someone else tries to use your database or you use the query only occasionally, a more informative prompt will avoid confusion and ensure the correct input.</p>
<h2>Obtaining a range</h2>
<p>Your parameter queries are not restricted to a single prompt. You can use them to find a range of values or obtain multiple criteria.</p>
<p>Here&#8217;s a query which answers the question, &#8220;Which countries have a population between x and y?&#8221; by prompting for a lower and an upper value:</p>
<p>1. If you still have the results of the <em>Population over X</em> query open, click the View button to return to Design View. If you closed the query after saving it, highlight it in the Queries list and click Design to open it in Design View.</p>
<p>2. In the Criteria cell of the Population column, add:</p>
<p><strong>AND &lt;[and a population under:]</strong></p>
<p>The full criterion should now read:</p>
<p><strong>&gt;[Show countries with a population over:] AND &lt;[and a population under:]</strong></p>
<p>3. Run the query and you&#8217;ll be prompted twice. Enter a lower limit in the first dialog and an upper limit in the second, then click OK to see the results.</p>
<p>4. From the File Menu choose Save As and name the query <em>Population Range</em>. By saving the query, you&#8217;ll be able to run it repeatedly, supplying different values each time.</p>
<p>This range query is similar to queries we&#8217;ve explored before, using two comparison operators (&gt; and &lt;) and the logical operator AND, but instead of using constant values, it substitutes two parameters obtained via dialogs.</p>
<h2>A range example using dates</h2>
<p>Let&#8217;s try one more range parameter query, this time using dates. This one will answer questions such as &#8220;Which countries joined the United Nations in the nth decade of 2000?&#8221;</p>
<ol>
<li>Create a new query in Design View.</li>
<li>Select the Countries table and click Add to add it to the query, then close the Show Tables dialog.</li>
<li>Drag the fields Name and JoinedUN onto the query grid.</li>
<li>In the Criteria cell of the JoinedUN column, type:</li>
</ol>
<blockquote><p><strong>&gt;[Joined UN after what date:] And &lt;[and before what date:]</strong></p></blockquote>
<ol start="5">
<li>Run this query and respond to the prompts by typing dates in the format dd/mm/yyyy. To view all countries that joined the United Nations during the nineties, for instance, enter the values <em>31/12/1989</em> and <em>1/1/2001</em>. If you use mm/dd format instead of dd/mm format for dates, you&#8217;ll want to make that first value 12/31/1989, of course.</li>
<li>Save this query and name it <em>Date Range</em>.</li>
</ol>
<p>You can, of course, use other operators (&lt;&gt;, =, &lt;= and so on) and logical expressions (OR, NOT) in range queries.</p>
<h2>Multiple values</h2>
<p>As well as matching a range of values in a single field, you can use parameter queries to match values in more than one field.</p>
<p>Let&#8217;s modify the Date Range query to let us not only specify a date range, but also limit the answer to countries over a certain population.</p>
<ol>
<li>Open the Date Range query in Design View.</li>
<li>Drag the Population field from the field list onto the query design grid to the right of the JoinedUN field. Our query now contains three fields – Name, JoinedUN and Population.</li>
<li>In the Criteria cell of the Population column, type:</li>
</ol>
<blockquote><p><strong>&gt;[and has a population over:]</strong></p></blockquote>
<ol start="4">
<li>Run the query.</li>
</ol>
<p>You&#8217;ll be presented with three prompts, asking for lower and upper dates and then a lower population limit. Note that this query in essence uses the AND logical operator to extend the query, so it&#8217;s the equivalent of: Show me countries that joined the UN between date x AND date y AND which have a population over n.</p>
<div id="attachment_1401" class="wp-caption alignnone" style="width: 622px"><a href="http://www.geekgirls.com/wp-content/uploads/running_a_parameter_query.jpg"><img class="size-full wp-image-1401" title="Running a parameter query" src="http://www.geekgirls.com/wp-content/uploads/running_a_parameter_query.jpg" alt="Running a parameter query" width="612" height="814" /></a><p class="wp-caption-text">Running a parameter query: A parameter query prompts the user for one or more criteria. In this example, the user specifies a population range and the query returns all matching records.</p></div>
<h2>Avoiding endless dialogs</h2>
<p>Try experimenting with parameter queries on your own. You&#8217;ll find they add enormous flexibility to the types of information you can wheedle out of your database. A tourist bureau, for example, can use them to provide information to the public on local events occurring this week. A small business can find all those customers who placed orders over a specific amount in the previous quarter. You&#8217;re sure to come up with instances where you can use them with your own data.</p>
<p>The only drawback with these queries is that you can end up peppering your users with endless, rather unattractive prompt boxes as you try to collect all the necessary parameters.</p>
<p>There&#8217;s good news. With a little fast footwork, you can avoid these dialogs and replace them with a single, good-looking dialog of your own design. I&#8217;ll show you how in the next article in this series.</p>
<p>Related posts:<ol>
<li><a href='http://www.geekgirls.com/2011/09/databasics-vi-exploring-query-types/' rel='bookmark' title='Databasics VI: Exploring query types'>Databasics VI: Exploring query types</a></li>
<li><a href='http://www.geekgirls.com/2011/09/databasics-v-getting-information-out/' rel='bookmark' title='Databasics V: Getting information out'>Databasics V: Getting information out</a></li>
<li><a href='http://www.geekgirls.com/2010/02/databasics-i-records-queries-keys-oh-my/' rel='bookmark' title='Databasics I: Records &amp; queries &amp; keys, oh my!'>Databasics I: Records &#038; queries &#038; keys, oh my!</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekgirls.com/2011/09/databasics-vii-parameter-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Databasics VI: Exploring query types</title>
		<link>http://www.geekgirls.com/2011/09/databasics-vi-exploring-query-types/</link>
		<comments>http://www.geekgirls.com/2011/09/databasics-vi-exploring-query-types/#comments</comments>
		<pubDate>Thu, 29 Sep 2011 23:35:46 +0000</pubDate>
		<dc:creator>rosevines</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Office Apps]]></category>
		<category><![CDATA[action queries]]></category>
		<category><![CDATA[calculated fields]]></category>
		<category><![CDATA[data access]]></category>
		<category><![CDATA[database design]]></category>
		<category><![CDATA[database queries]]></category>
		<category><![CDATA[dbms]]></category>
		<category><![CDATA[microsoft access]]></category>
		<category><![CDATA[parameter queries]]></category>
		<category><![CDATA[queries]]></category>
		<category><![CDATA[select queries]]></category>

		<guid isPermaLink="false">http://www.geekgirls.com/?p=1386</guid>
		<description><![CDATA[Learn how to start building more complex database queries, such as comparative and action queries, using Microsoft Access.
   ]]></description>
			<content:encoded><![CDATA[<div class="woo-sc-box info   full">Note: I&#8217;m working on updating this series of articles. This article is really showing its age. All the examples are for  Microsoft Access 2000/2003, and things have changed a lot since then. While the concepts are still the same and you can learn a lot from those, the step-by-steps are quite different if you&#8217;re working with a newer database program such as Access 2007 or 2010. I plan to produce a new article with Access 2010 examples in the next month or so, so if you&#8217;re interested, check back in a while.</div>
<p><span class="dropcap">I</span><!--/.dropcap-->n the previous tutorial we used Query By Example (QBE) on a database of countries to answer questions such as &#8216;Which countries are members of the UN Security Council?&#8217; and &#8216;Which countries have a population over 20 million and an area over 2 million square kilometres?&#8217; These are simple queries which we created using QBE and basic operators such as &gt; (greater than).</p>
<p>It&#8217;s possible to ask more complex questions, such as:</p>
<ul>
<li>Which country joined the UN most recently?</li>
<li>Which countries are in the top 5% in terms of area?</li>
<li>Which five countries have the lowest population density?</li>
</ul>
<p>The way you tackle these queries depends on which database program you use. I&#8217;ll be using Microsoft Access in the examples, as it has querying tools that leave most other PC databases in the shade. You can download <a href="http://www.box.net/shared/xz73meuimr9yqgcgqods" target="_blank">a sample copy of the database</a> to try this out for yourself.</p>
<p><strong>Comparative queries</strong></p>
<p>The simple queries we explored last time did nothing more than find a matching value (Name=Namibia), or compare a value in a field to a constant (Population&lt;=20000000).</p>
<p>We can take this a step further and run queries that will compare one piece of data against the rest of the data in the table. These are real bread-and-butter queries. For instance, in a sales database, you might want the answers to questions such as: Which region had the lowest sales for the quarter? Who are our five best salespeople? Which salespeople performed better this quarter than last?</p>
<p>To ask these types of questions, you need to use something a little more sophisticated than QBE. In Access, you use the Query Designer.</p>
<p><strong>Using Access&#8217;s Query Designer</strong></p>
<p>Let&#8217;s start with the question, Which country joined the UN most recently? Here&#8217;s how to get the answer:</p>
<ol start="1">
<li>Open the Countries database in Access.</li>
<li>In the main Database Window, click Queries in the Objects list.</li>
<li>Double-click Create Query In Design View. A Query window opens together with a Show Table box containing all the tables in our database.</li>
<li>Select the Countries table, click Add, and then click Close. This adds the Countries database to the Query window, so we can work with it.</li>
<li>Click Name in the list of fields and then hold down the Ctrl key and click JoinedUN (you may need to scroll to see it). Drag the two fields to the left-hand column in the query design grid. The two fields will appear side by side in the grid. The top row in the grid shows the field name; the second row shows which table the field belongs to (we&#8217;ll get into multi-table queries at a later date); the third row lets you specify a sort order for the results; the fourth row (Show) specifies whether you want the contents of this field displayed in the results; the fifth and sixth rows specify criteria for selecting records.</li>
<li>In the JoinedUN column on the grid, click in the Sort row and, from the drop-down box, select Descending. This tells Access we want the table sorted with the most recent records (those with the&#8217;largest&#8217; dates) first.</li>
<li>In the toolbar, beside the Sigma button you&#8217;ll see an empty drop-down box. This is the Top Values button. Type 1 in this box. We&#8217;re looking for the &#8216;top date&#8217;, so to speak – the most recent date in the JoinedUN field – so we want the top, single value.</li>
<li>Run the query by clicking the Run button (it has an exclamation mark on it). You&#8217;ll see the result is Tuvalu, which joined the UN on the 5th September, 2000.</li>
</ol>
<blockquote><p><strong><div class="woo-sc-box normal   ">geekgirl:geopolitical update</strong></p>
<p>In fact, if you run this query on the updated version of the Countries database I now have online, you&#8217;ll find the answer is not Tuvalu, but South Sudan. East Timor, Switzerland, Montenegro and South Sudan have all joined the United Nations since I first wrote this article. While I&#8217;ve left the article text the same, I have updated the database to reflect these recent additions to the United Nations family (I haven&#8217;t had a chance to update the population figures, alas).</p>
<p>These changes show how useful database queries can be: Provided the data in your database is accurate and up to date, you can use a query you created years ago on your current data and get an accurate and up-to-date result.</div></p></blockquote>
<p><strong>Saving and re-running queries</strong></p>
<p>Click the Close box on the Query window. You&#8217;ll be asked whether you want to save the query. Click Yes, name the query <em>Most Recent Member</em> and click OK.</p>
<p>Why should you save a query? After all, you already know the answer to your question is Tuvalu. But then, the answer won&#8217;t always be Tuvalu. Say Switzerland decides to join the UN this year, or a new country emerges from one of the bubbling spots on the globe and becomes a new member? (See the Geopolitical Update above.)</p>
<p>By saving the query, you can re-run it at any time and find the up-to-date answer to your question. This may seem a fairly trivial operation with our simple example, but when you&#8217;re creating complex queries or, more importantly, when your data changes regularly, saved queries eliminate a lot of work.</p>
<p>To use your saved query, simply double-click it in the Queries list.</p>
<p><strong>Query types</strong></p>
<p>The Most Recent Member query you created is called a Select query. Access lets you create five different types of query:</p>
<p><strong>Select queries.</strong> Used to retrieve data from one or more tables and display the results in a datasheet, which you can save or modify. You can also use Select queries to group records and calculate sums, averages and so on.</p>
<p><strong>Parameter queries.</strong> For creating on-the-fly queries which prompt the user for criteria at the time the query is run. For example, you can create a parameter query that answers the question: Which countries have a population greater than X and less than Y? Each time you run the query, it will prompt you for the values of X and Y. Thus you can use the same query repeatedly to discover different information.</p>
<p><strong>Crosstab queries.</strong> Used to summarise data from one field and group it in tabular fashion according to two criteria.</p>
<p><strong>Action queries.</strong> Queries that make changes to the records in a table. There are four type of action queries: <em>Delete queries</em> remove records from a table; <em>Update queries</em> make global changes to a group of records in a table; <em>Append queries</em> add records from one or more tables to the end or one or more tables; <em>Make-table queries</em> create a new table from all or part of the data in an existing table.</p>
<p><strong>SQL queries.</strong> A query created using SQL, which is a highly advanced querying language. SQL queries give you enormous flexibility, but require a high degree of expertise to use effectively.</p>
<p><strong>A select query example</strong></p>
<p>Let&#8217;s quickly run through a second select query, answering the question: Which countries are in the top 5% in terms of area?</p>
<ol start="1">
<li>Double-click Create Query In Design View.</li>
<li>Select the Countries table, click Add, and then click Close.</li>
<li>Click Name in the list of fields, Ctrl-click Area, and drag the two fields to the grid.</li>
<li>In the Area column, choose Descending from the Sort drop-down box.</li>
<li>In the Top Values box on the toolbar, type 5% (or select it from the drop-down list).</li>
<li>Click Run to execute the query.</li>
</ol>
<p><strong>Calculated fields</strong></p>
<p>How about the question: Which five countries have the lowest population density? We don&#8217;t have a Population Density field in our table, so how can we calculate it?</p>
<p>We do it by creating a new field which becomes part of our results (note, though, that the field does not become part of the existing table structure). Here&#8217;s how:</p>
<ol start="1">
<li>Double-click Create Query In Design View.</li>
<li>Select the Countries table, click Add, and then click Close.</li>
<li>Drag the Name and Population fields onto the grid.</li>
<li>In the top row of the empty third column on the grid, type:</li>
</ol>
<p>PopDensity: population/area</p>
<p>and press Enter. We&#8217;re creating a new column (PopDensity) whose values will be calculated by dividing each country&#8217;s population by its area. By the way, if you can&#8217;t see the entire contents of this column, drag the right-hand edge of the column header to the right to resize it.</p>
<ol start="5">
<li>In the Top Values box on the toolbar, type 5.</li>
<li>Click the Totals button (it has the Sigma sign on it). A new Totals row appears in the grid.</li>
<li>In the PopDensity column choose Ascending from the Sort box.</li>
<li>Click Run to execute the query.</li>
</ol>
<p>The results show the names, population and population density for the world&#8217;s least populated countries.</p>
<div id="attachment_1390" class="wp-caption alignnone" style="width: 621px"><a href="http://www.geekgirls.com/wp-content/uploads/calculated_query.jpg"><img class="size-full wp-image-1390  " title="A select query" src="http://www.geekgirls.com/wp-content/uploads/calculated_query.jpg" alt="A select query" width="611" height="387" /></a><p class="wp-caption-text">This Select query in Access finds the five most sparsely populated countries in the world using a calculated field, PopDen</p></div>
<p>&nbsp;</p>
<p><strong>An action query</strong></p>
<p>Let&#8217;s finish up by converting that last query into an action query. This query doesn&#8217;t merely provide us with the results; it saves them in a new table which includes a PopDensity field in its structure. The original table remains intact and unchanged; it&#8217;s the new results table which has the extended structure.</p>
<ol start="1">
<li>If you have the query results still showing, click the View button at the left end of the toolbar to view the query in design mode once more. If you have already closed the query, recreate it.</li>
<li>Click the down arrow on the Query Type button on the toolbar and select Make-Table Query.</li>
<li>Name the query <em>Sparsely Populated</em>, leave the Current Database option selected, and click OK.</li>
<li>Run the query and, when prompted, say Yes to create the new table.</li>
<li>Close the query, click Tables in the Objects panel, and you&#8217;ll see a new table called Sparsely Populated. Double-click it to see the query results in the new table. You&#8217;ll notice the new table contains five records (the most sparsely populated five countries) and three fields – Name, Population and PopDensity.</li>
</ol>
<p>&nbsp;</p>
<p>Related posts:<ol>
<li><a href='http://www.geekgirls.com/2011/09/databasics-vii-parameter-queries/' rel='bookmark' title='Databasics VII: Parameter Queries'>Databasics VII: Parameter Queries</a></li>
<li><a href='http://www.geekgirls.com/2011/09/databasics-v-getting-information-out/' rel='bookmark' title='Databasics V: Getting information out'>Databasics V: Getting information out</a></li>
<li><a href='http://www.geekgirls.com/2010/02/databasics-i-records-queries-keys-oh-my/' rel='bookmark' title='Databasics I: Records &amp; queries &amp; keys, oh my!'>Databasics I: Records &#038; queries &#038; keys, oh my!</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekgirls.com/2011/09/databasics-vi-exploring-query-types/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Databasics V: Getting information out</title>
		<link>http://www.geekgirls.com/2011/09/databasics-v-getting-information-out/</link>
		<comments>http://www.geekgirls.com/2011/09/databasics-v-getting-information-out/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 00:10:03 +0000</pubDate>
		<dc:creator>rosevines</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[database design]]></category>
		<category><![CDATA[database queries]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[microsoft access]]></category>
		<category><![CDATA[queries]]></category>
		<category><![CDATA[query language]]></category>
		<category><![CDATA[relational database]]></category>
		<category><![CDATA[relational database design]]></category>

		<guid isPermaLink="false">http://www.geekgirls.com/?p=1366</guid>
		<description><![CDATA[Queries let you make sense of the data you have stored in your database. Learn how to create simple, yet remarkably useful, queries by example.]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap">T</span><!--/.dropcap-->he whole point of using a database is to turn <em>data</em> into <em>information</em>. Data are facts which have no inherent meaning; information is data put into context to convey meaning.</p>
<p>Think of a customer database containing information such as customer names, addresses and phone numbers. In the State field, you&#8217;ll find the values QLD, VIC, WA, NT, NSW and so on repeated over and over (that is, if you live in Australia – adjust the values to suit your locale if you live elsewhere). Without a context, this list of state abbreviations is meaningless. Put a question to your database such as &#8220;What percentage of our customers live in WA?&#8221;, however, and the resulting answer is useful, meaningful information.</p>
<div class="woo-sc-box info   full">Note: I&#8217;m working on updating this series of articles. This article is getting long in the tooth and although the general information and techniques still apply, all the examples are in old versions of Microsoft Access and FileMaker Pro.</div>
<h2>Queries</h2>
<p>We&#8217;ve spent the last few tutorials looking at how to structure a database and how to design data entry forms. It&#8217;s vital to get this design phase right, otherwise you can&#8217;t be sure any information you get from your database will be trustworthy. Additionally, you want to make sure data entry is as easy as possible not only for the comfort of the people stuck with this task, but also to help ensure data is entered correctly. The more attention you pay to the design phase, the better. But the really interesting and useful stuff happens when you start trying to get information <em>out</em> of your database. This is where you turn the stored data into information – a process called <em>querying</em>.</p>
<p>A query is a question you &#8216;ask&#8217; a database in order to get information in a useful format. As databases don&#8217;t understand plain English (although there have been attempts to design database programs that do), you have to phrase your question according to rules. The rules are usually referred to as a <em> query language</em>.</p>
<h2>Query language</h2>
<p>If this sounds a little intimidating, don&#8217;t let it faze you. Chances are you&#8217;re already familiar with using a simple form of query language on the Internet. That&#8217;s because Web search engines are, in fact, a type of database. When you type a search phrase such as:</p>
<p>piano steinway bechstein</p>
<p>into a Web search engine, the search engine queries its stored database of websites using the three keywords you&#8217;ve supplied. Your search will turn up all web pages that contain the word piano or Steinway or Bechstein, or any combination of the three words.</p>
<p>If you&#8217;ve done any advanced web searching, you may have tried creating Boolean search queries (named after George Boole, an English mathematician who helped develop symbolic logic). In Boolean queries you use logical operators such as AND (or &amp;), OR and NOT to create more precise searches. Like this:</p>
<p>piano AND (steinway OR bechstein)</p>
<p>This search query will locate web pages that contain the words <em>piano</em> and <em>Steinway</em>, or the words <em>piano</em> and <em>Bechstein</em>, or all three words, but it won&#8217;t turn up pages that mention Steinway or Bechstein but don&#8217;t contain the word piano.</p>
<p>Querying a database is similar. The query language may be more extensive and you&#8217;ll be given tools that make the job very precise, but if you can use a search engine you won&#8217;t have too many problems learning how to query a database.</p>
<p>The actual way you create a query depends on the database program you use. For example, the database in Microsoft Works uses an ultra-simple, fill-in-the-blanks approach called <em>query by example</em> (QBE for short). Microsoft Access also has a QBE feature, called <em> Filter By Form</em>. Filter by Form is the easiest way to pull information out of an Access database, but for more complex queries and to create queries which can be re-run at any time, you&#8217;ll probably use Access&#8217;s Query Builder, a highly sophisticated query tool. Alpha Five and FileMaker Pro also offer a choice of methods to query your data, including QBE.</p>
<h2>Making comparisons</h2>
<p>You need more than Boolean operators to create a query. You also need a way to compare one value to another, and that&#8217;s where <em>comparison operators</em> come into play. In combination with Boolean operators, they let us create questions about our data that the database can understand.</p>
<p>You&#8217;ll be familiar with comparison operators from school arithmetic. Here&#8217;s a list of them.</p>
<table width="340" border="0" cellspacing="1">
<tbody>
<tr>
<td width="50%">Operator</td>
<td width="50%">Meaning</td>
</tr>
<tr>
<td width="50%"><strong>=</strong></td>
<td width="50%"><strong>is equal to</strong></td>
</tr>
<tr>
<td width="50%"><strong>&lt;&gt;</strong></td>
<td width="50%"><strong>is not equal to</strong></td>
</tr>
<tr>
<td width="50%"><strong>&gt;</strong></td>
<td width="50%"><strong>is greater than</strong></td>
</tr>
<tr>
<td width="50%"><strong>&gt;=</strong></td>
<td width="50%"><strong>is greater than or equal to</strong></td>
</tr>
<tr>
<td width="50%"><strong>&lt;</strong></td>
<td width="50%"><strong>is less than</strong></td>
</tr>
<tr>
<td width="50%"><strong>&lt;=</strong></td>
<td width="50%"><strong>is less than or equal to</strong></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>To understand how to use comparison operators, imagine a database containing a country table which includes the following fields (the data type of each field is shown in parentheses):</p>
<table width="340" border="0" cellspacing="1">
<tbody>
<tr>
<td width="50%">Fieldname</td>
<td width="50%">Field type</td>
</tr>
<tr>
<td width="50%"><strong>Name</strong></td>
<td width="50%"><strong>text</strong></td>
</tr>
<tr>
<td width="50%"><strong>Area</strong></td>
<td width="50%"><strong>numeric</strong></td>
</tr>
<tr>
<td width="50%"><strong>Population</strong></td>
<td width="50%"><strong>numeric</strong></td>
</tr>
<tr>
<td width="50%"><strong>UnitedNationsMember</strong></td>
<td width="50%"><strong>yes/no</strong></td>
</tr>
<tr>
<td width="50%"><strong>JoinedUN</strong></td>
<td width="50%"><strong>date</strong></td>
</tr>
<tr>
<td width="50%"><strong>SecurityCouncilMember</strong></td>
<td width="50%"><strong>yes/no</strong></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>(Note: You&#8217;ll find a link at the end of this article which will let you download a copy of the Countries database containing 194 country records, which you can use to try out your own queries.)</p>
<div class="woo-sc-box note   ">Since I first wrote this article, the world has moved on. Switzerland, Timor-Leste, Montenegro and South Sudan have joined the United Nations (welcome to all of you!), I have updated the sample database to reflect these changes, but have left the text of these articles intact, partly for historical interest but also as a clear example of why database queries are so wonderful: Even though the data in the Countries database has changed, well-constructed queries will continue to provide correct results. Unfortunately, I haven&#8217;t had time to update the population details in the database, so take all results with a grain of salt!</div>
<h2>Using operators to make comparisons</h2>
<p>Consider the types of information you might try to get from the database. Here are some examples:</p>
<ol>
<li>What&#8217;s the population of Namibia?</li>
<li>Which countries are members of the Security Council and when did they join the UN?</li>
<li>Which countries are not members of the UN?</li>
<li>Which countries have a population under 20 million?</li>
<li>Which countries names begin with A, B or C?</li>
<li>Which countries have a population over 20 million or an area of over 2 million square kilometres?</li>
<li>Which countries have a population over 20 million and an area over 2 million square kilometres?</li>
</ol>
<p>Take that first question – What&#8217;s the population of Namibia? To rephrase it so our database can understand what we&#8217;re asking, we make use of the = (is equal to) operator:</p>
<p><em>Show me the record in the Countries table where the Name field=&#8221;Namibia</em>&#8220;<em>, and display the value of the Population field for that record.</em></p>
<p>So, here are paraphrases for all of our questions:</p>
<ol>
<li>Show me the record in the Countries table where the Name field=&#8221;Namibia&#8221;, and display the value of the Population field for that record.</li>
<li>Show me the records where SecurityCouncilMember=Yes, and display the value of the JoinedUN field for the matching records</li>
<li>Show me the records where UnitedNationsMember&lt;&gt;Yes</li>
<li>Show me the records where Population&lt;20,000,000</li>
<li>Show me the records where Name&lt;&#8221;D&#8221;</li>
<li>Show me the records where Population&gt;20,000,000 OR Area&gt;=2,000,000</li>
<li>Show me the records where Population&gt;20,000,000 AND Area&gt;=2,000,000</li>
</ol>
<h2>Broadening and narrowing the scope</h2>
<p>Most of these are self explanatory, but let&#8217;s take a closer look at the last two where we&#8217;ve combined comparison and Boolean operators.</p>
<p>Example 6 displays all countries which <em>either</em> have a population of more than 20 million <em>or</em> which have an area of 2 million square miles or more <em>or</em> which have both a population over a million and an area that&#8217;s 2 million square miles or more.</p>
<p>Example 7 displays only those countries which have <em>both</em> a population of over 20 million <em>and</em> an area of 2 million square miles or greater.</p>
<p>The OR operator broadens the scope of the query to include more records. The AND operator narrows the scope of the query.</p>
<p>One other thing you may have noticed in example 5: You&#8217;re not restricted to using operators such as &lt; and &gt; with numbers alone. They work equally well with text and dates.</p>
<h2>Using QBE</h2>
<p>If all these operators have your head in a spin, never fear! Most database programs do all the hard work of concocting queries in the background, while you merely fill in a form to show the type of information you&#8217;re after. Nevertheless, if you want to create more complex queries or milk the most subtle information from your data, you&#8217;ll certainly put this knowledge to use. Even using query by example you&#8217;ll find yourself using comparison operators regularly.</p>
<p>Let&#8217;s step through a query by example in Microsoft Access and in FileMaker Pro. [You can download a copy of the <a href="http://www.box.net/shared/xz73meuimr9yqgcgqods" target="_blank">sample Access database</a> or the <a href="http://www.box.net/shared/tlcchihzzlrus894hojg" target="_blank">FileMaker database</a> to try it for yourself. Note that you'll get a warning when you try to download the Access sample (countries.mdb) that it is an executable file. The file is completely safe, but if you're concerned, simply scan the file with your antivirus software before opening it.]</p>
<h2>An example in Access</h2>
<ol>
<li>Double-click the Countries table in the Database window to open the table in Datasheet View. This view shows a list of all the records in the database.</li>
<li>Click the Filter By Form button on the toolbar (it has a picture of a funnel on top of a form). Your records will disappear and you&#8217;ll be presented with a single blank record.</li>
<li> In the name field type <em>Namibia</em> and then click Apply Filter (the button with the filter on it). The matching record will be displayed.</li>
<li>Click the Apply Filter button (it&#8217;s now called the Remove Filter button) once more to redisplay all your records.</li>
</ol>
<h2>A more complex example</h2>
<p>Now, for a slightly more complex query, let&#8217;s search for countries that have a population of 50 million or less which are also members of the UN Security Council:</p>
<ol>
<li>Keep the Countries database open in Datasheet View.</li>
<li>Click the Filter By Form button.</li>
<li>If &#8220;Namibia&#8221; still shows in the Name field, delete it.</li>
<li>In the Population field, type <em>&lt;=50000000</em> (don&#8217;t put commas in the number) and tick the box in the SecurityCouncilMember field.</li>
<li>Click the Apply Filter button. You should see the records for Mali, Jamaica and the Ukraine.</li>
</ol>
<p>Query By Example is delightfully simple, but it still has its pitfalls. For example, if you fail to tick (place a checkmark in) the UnitedNationsMember field in this Access query, you&#8217;ll get no matches. That&#8217;s because you&#8217;d be asking for records where the country is <em>not</em> a member of the United Nations but <em>is</em> a member of the UN Security Council, which, of course, cannot be. Click the image to see a full-sized version.</p>
<h2>The same thing in FileMaker Pro</h2>
<ol>
<li>Open the Countries database and press Ctrl-F to go into Find Mode.</li>
<li>Type <em>Namibia</em> in the Name field and press Enter to display the matching record. Note that the number of matching records is displayed on the left.</li>
<li>Click the Show All Records button (the one with an eye) to redisplay all your records.</li>
</ol>
<p>For the second query:</p>
<ol>
<li>Press Ctrl-F to enter Find Mode.</li>
<li>Tab down to the Population field and type <em>&lt;=50000000</em>.</li>
<li>Tab to the SecurityCouncilMember field, type <em>Yes</em> and press Enter to display the matching records. Flip through the matches by clicking the Records icon on the left.</li>
</ol>
<h2>More complex still</h2>
<p>So far we&#8217;ve merely skimmed the surface of using queries. The QBE examples show how easy it is to get useful information out of a database, but there&#8217;s a whole lot more to querying, including the ability to save and re-use queries, and to create queries that prompt the user for information. We&#8217;ll examine queries in more detail in the next tutorial.</p>
<p>Related posts:<ol>
<li><a href='http://www.geekgirls.com/2011/09/databasics-vi-exploring-query-types/' rel='bookmark' title='Databasics VI: Exploring query types'>Databasics VI: Exploring query types</a></li>
<li><a href='http://www.geekgirls.com/2011/09/databasics-vii-parameter-queries/' rel='bookmark' title='Databasics VII: Parameter Queries'>Databasics VII: Parameter Queries</a></li>
<li><a href='http://www.geekgirls.com/2010/02/databasics-i-records-queries-keys-oh-my/' rel='bookmark' title='Databasics I: Records &amp; queries &amp; keys, oh my!'>Databasics I: Records &#038; queries &#038; keys, oh my!</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekgirls.com/2011/09/databasics-v-getting-information-out/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Which database series should I read?</title>
		<link>http://www.geekgirls.com/2011/09/which-database-series-should-i-read/</link>
		<comments>http://www.geekgirls.com/2011/09/which-database-series-should-i-read/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 22:41:27 +0000</pubDate>
		<dc:creator>rosevines</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Office Apps]]></category>

		<guid isPermaLink="false">http://www.geekgirls.com/?p=1363</guid>
		<description><![CDATA[Not sure which series of database tutorials on Geekgirl's Plain English Computing is right for you, Databasics or Databases from Scratch? Here's a quick description of their contents.]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap">I</span><!--/.dropcap-->n the beginning there was a single series of database tutorials on this site. Now there are two. Which one should you read?</p>
<p>If you&#8217;re looking for a nice, slow approach to learning databases, choose <em>Databasics.</em> It will help you learn the lingo, start with simple stuff, and eventually take on something a little more challenging. It&#8217;s a series in development, so you can munch on some new information, go away and put it to use, then come back in a month or so and try something new. It&#8217;s also a very practical series. In Databasics, although the techniques and general advice apply no matter which database product you use, you&#8217;ll find specific advanced examples feature Microsoft Access.</p>
<p>If, on the other hand, you want to dive right into the nitty gritty of database design, try the companion series of articles, <em>Databases from Scratch.</em> While there&#8217;s some overlap between the two series, <em>Databases from Scratch</em> moves onto advanced topics, such as relational database design, more rapidly and restricts its topic matter to design issues. <em>Databasics</em> wanders farther afield, and includes (or will eventually include) tutorials on queries, reports, forms design, application development as well as basic design principles. </p>
<p>Related posts:<ol>
<li><a href='http://www.geekgirls.com/2007/11/the-year-of-the-database/' rel='bookmark' title='The year of the database'>The year of the database</a></li>
<li><a href='http://www.geekgirls.com/2011/09/databases-from-scratch-ii-simple-database-design/' rel='bookmark' title='Databases from scratch II: Simple Database Design'>Databases from scratch II: Simple Database Design</a></li>
<li><a href='http://www.geekgirls.com/2010/02/a-database-dictionary/' rel='bookmark' title='A database dictionary'>A database dictionary</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekgirls.com/2011/09/which-database-series-should-i-read/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Databases from scratch III: The Design Process</title>
		<link>http://www.geekgirls.com/2011/09/databases-from-scratch-iii-relational-design-process/</link>
		<comments>http://www.geekgirls.com/2011/09/databases-from-scratch-iii-relational-design-process/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 22:32:45 +0000</pubDate>
		<dc:creator>rosevines</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Office Apps]]></category>
		<category><![CDATA[database design]]></category>
		<category><![CDATA[dbms]]></category>
		<category><![CDATA[microsoft access]]></category>
		<category><![CDATA[relational database]]></category>
		<category><![CDATA[relational database design]]></category>

		<guid isPermaLink="false">http://www.geekgirls.com/?p=1348</guid>
		<description><![CDATA[This third article in the Databases from Scratch series delves into the database heartland by exploring relational database design.]]></description>
			<content:encoded><![CDATA[<p><em>This third article in the series delves into the database heartland by exploring relational database design.</em></p>
<p><span class="dropcap">O</span><!--/.dropcap-->ne of the best ways to understand the importance of good database design is to start with an all-in-one, flat-file table design and then toss in some sample data to see what happens. By analysing the sample data, you&#8217;ll be able to identify problems caused by the initial design. You can then modify the design to eliminate the problems, test some more sample data, check for problems, and re-modify, continuing this process until you have a consistent and problem-free design.</p>
<p>Once you grow accustomed to the types of problems poor table design can create, hopefully you&#8217;ll be able to skip the interim steps and jump immediately to the final table design.</p>
<h2>A sample design process</h2>
<p>Let&#8217;s step through a sample database design process.</p>
<p>We&#8217;ll design a database to keep track of students&#8217; sports activities. We&#8217;ll track each activity a student takes and the fee per semester to do that activity.</p>
<p><strong>Step 1: Create an Activities table</strong> containing all the fields: student&#8217;s name, activity and cost. Because some students take more than one activity, we&#8217;ll make allowances for that and include a second activity and cost field. So our structure will be: Student, Activity 1, Cost 1, Activity 2, Cost 2</p>
<p><strong>Step 2: Test the table with some sample data.</strong> When you create sample data, you should see what your table <em>lets you get away with.</em> For instance, nothing prevents us from entering the same name for different students, or different fees for the same activity, so do so. You should also imagine trying to ask questions about your data and getting answers back (essentially <em>querying</em> the data and producing reports). For example, how do I find all the students taking tennis?</p>
<p><strong>Step 3: Analyse the data. </strong>In this case, we can see a glaring problem in the first field. We have two John Smiths, and there&#8217;s no way to tell them apart. We need to find a way to identify each student uniquely.</p>
<p><a href="http://www.geekgirls.com/wp-content/uploads/activities_01.gif"><img class="alignnone size-full wp-image-1350" title="A flawed table design" src="http://www.geekgirls.com/wp-content/uploads/activities_01.gif" alt="A flawed table design" width="376" height="134" /></a></p>
<h2>Uniquely identify records</h2>
<p>Let&#8217;s fix the glaring problem first, then examine the new results.</p>
<p><strong>Step 4: Modify the design. </strong>We can identify each student uniquely by giving each one a unique ID, a new field that we add, called ID. We scrap the Student field and substitute an ID field. Note the asterisk (*) beside this field in the table below: it signals that the ID field is a <em>key field</em>, containing a unique value in each record. We can use that field to retrieve any specific record. When you create such a key field in a database program, the program will then prevent you from entering duplicate values in this field, safeguarding the uniqueness of each entry.</p>
<p>Our table structure is now: ID, Activity 1, Cost 1, Activity 2, Cost 2</p>
<p>While it&#8217;s easy for the computer to keep track of ID codes, it&#8217;s not so useful for humans. So we&#8217;re going to introduce a second table that lists each ID and the student it belongs to. Using a database program, we can create both table structures and then link them by the common field, ID. We&#8217;ve now turned our initial <em>flat-file</em> design into a <em>relational database</em>: a database containing multiple tables linked together by key fields. If you were using a database program that can&#8217;t handle relational databases, you&#8217;d basically be stuck with our first design and all its attendant problems. With a relational database program such as Microsoft Access or FileMaker, you can create as many tables as your data structure requires.</p>
<p>The Students table would normally contain each student&#8217;s first name, last name, address, age and other details, as well as the assigned ID. To keep things simple, we&#8217;ll restrict it to name and ID, and focus on the Activities table structure.</p>
<p><strong>Step 5: Test the table with sample data.</strong></p>
<p><a href="http://www.geekgirls.com/wp-content/uploads/activities_02.gif"><img class="alignnone size-full wp-image-1351" title="Splitting the flat file into two related tables" src="http://www.geekgirls.com/wp-content/uploads/activities_02.gif" alt="Splitting the flat file into two related tables" width="340" height="277" /></a></p>
<p><strong>Step 6: Analyse the data.</strong> There&#8217;s still a lot wrong with the Activities table:</p>
<ol>
<li>Wasted space. Some students don&#8217;t take a second activity, and so we&#8217;re wasting space when we store the data. It doesn&#8217;t seem much of a bother in this sample, but what if we&#8217;re dealing with thousands of records?</li>
<li>Addition anomalies. What if #219 (we can look him up and find it&#8217;s Mark Antony) wants to do a third activity? School rules allow it, but there&#8217;s no space in this structure for another activity. We can&#8217;t add another record for Mark, as that would violate the unique key field ID, and it would also make it difficult to see all his information at once.</li>
<li>Redundant data entry. If the tennis fees go up to $39, we have to go through <em>every</em> record containing tennis and modify the cost in each and every one.</li>
<li>Querying difficulties. It&#8217;s difficult to find all people doing swimming: we have to search through Activity 1 <em>and</em> Activity 2 to make sure we catch them all.</li>
<li>Redundant information. If 50 students take swimming, we have to type in both the activity and its cost each time.</li>
<li>Inconsistent data. Notice that there are conflicting prices for swimming? Should it be $15 or $17? This happens when one record is updated and another isn&#8217;t.</li>
</ol>
<h2>Eliminate recurring fields</h2>
<p>Our simple Students table is fine, so we&#8217;ll keep it. But there&#8217;s so much wrong with the Activities table let&#8217;s try to fix it in stages.</p>
<p><strong>Step 7: Modify the design.</strong> We can fix the first four problems by creating a separate record for each activity a student takes, instead of having one record for all the activities a student takes.</p>
<p>First we eliminate the Activity 2 and Cost 2 fields. Then we need to adjust the table structure so we can enter multiple records for each student. To do that, we redefine the key so that it consists of two fields, ID and Activity. As each student can only take an activity once, this combination gives us a unique key for each record.</p>
<p>Our Activities table has now been simplified to: ID, Activity, Cost. Note how the new structure lets students take any number of activities – they&#8217;re no longer limited to two.</p>
<p><strong>Step 8: Test sample data.</strong></p>
<p><a href="http://www.geekgirls.com/wp-content/uploads/activities_03.gif"><img class="alignnone size-full wp-image-1352" title="Eliminating recurring fields" src="http://www.geekgirls.com/wp-content/uploads/activities_03.gif" alt="Eliminating recurring fields" width="392" height="207" /></a></p>
<p><strong>Step 9: Analyse the data.</strong> We know we still have the problems with redundant data (activity fees repeated) and inconsistent data (what&#8217;s the correct fee for swimming?). We need to fix these things, which are both problems when it comes to data entry &#8211; editing or modifying records.</p>
<h2>Eliminate data entry anomalies</h2>
<p>As well, we should check that other data entry processes, such as adding or deleting records, will function correctly too.</p>
<p>If you look closely, you&#8217;ll find that there are potential problems when we add or delete records:</p>
<ul>
<li>Insertion anomalies. What if our school introduces a new activity, such as sailing, at $50. Where can we store this information? With our current design we can&#8217;t until a student signs up for the activity.</li>
<li>Deletion anomalies. If Mark Antony (#219) transfers to another school, all the information about golf disappears from our system, as he is the only student taking this activity.</li>
</ul>
<p><strong>Step 10: Modify the design. </strong> The cause of all our remaining problems is that we have a non-key field (cost) which is dependent on only part of the key (activity). Check it out for yourself: The cost of each activity is <em> not</em> dependent on the student&#8217;s ID, which is part of our composite key (ID + Activity). The cost of tennis, for example, is $36 for each and every student who takes the sport – so the student&#8217;s ID has no bearing on the value contained in this field. The cost of an activity is purely dependent on the activity itself &#8211; it&#8217;s dependent on only part of the combined ID/activity key. This is a design no-no, and it&#8217;s causing us problems. By checking our table structures and ensuring that <em> every non-key field is dependent on the whole key,</em> we will eliminate the rest of our problems.</p>
<p>Our final design will thus contain three tables: the Students table (Student, ID), a Participants table (ID, Activity), and a modified Activities table (Activity, Cost).</p>
<p>If you check these tables, you&#8217;ll see that each non-key value depends on the whole key: the student name is entirely dependent on the ID; the activity cost is entirely dependent on the activity. Our new Participants table essentially forms a union of information drawn from the other two tables, and each of its fields is part of the key. The tables are linked by key fields: the Students table:ID corresponds to the Participants table:ID; the Activities table:Activity corresponds to the Participants table:Activity.</p>
<p><strong>Step 11: Test sample data.</strong></p>
<p><a href="http://www.geekgirls.com/wp-content/uploads/activities_04.gif"><img class="alignnone size-full wp-image-1353" title="A workable design" src="http://www.geekgirls.com/wp-content/uploads/activities_04.gif" alt="A workable design" width="381" height="355" /></a></p>
<p><strong>Step 12: Analyse the results.</strong> This looks good:</p>
<ul>
<li>No redundant information. You need only list each activity fee once.</li>
<li>No inconsistent data. There&#8217;s only one place where you can enter the price of each activity, so there&#8217;s no chance of creating inconsistent data. Also, if there&#8217;s a fee rise, all you need to do is update the cost in one place.</li>
<li>No insertion anomalies. You can add a new activity to the Activities table without a student signing up for it.</li>
<li>No deletion anomalies. If Mark Antony (#219) leaves, you still retain the details about the golfing activity.</li>
</ul>
<p>Keep in mind that to simplify the process and focus on the relational aspects of designing our database structure, we&#8217;ve placed the student&#8217;s name in a single field. This is not what you&#8217;d normally do: you&#8217;d divide it into firstname, lastname (and initials) fields. Similarly, we&#8217;ve excluded other fields that you would normally store in a student table, such as date of birth, address, parents&#8217; names and so on.</p>
<h2>A summary of the design process</h2>
<p>Although your ultimate design will depend on the complexity of your data, each time you design a database, make sure you do the following:</p>
<ul>
<li>Break composite fields down into constituent parts. Example: Name becomes lastname and firstname.</li>
<li>Create a key field which uniquely identifies each record. You may need to create an ID field (with a lookup table that shows you the values for each ID) or use a composite key.</li>
<li>Eliminate repeating groups of fields. Example: If your table contains fields Location 1, Location 2, Location 3 containing similar data, it&#8217;s a sure warning sign.</li>
<li>Eliminate record modification problems (such as redundant or inconsistent data) and record deletion and addition problems by ensuring each non-key field depends on the <em>entire</em> key. To do this, create a separate table for any information that is used in multiple records, and then use a key to link these tables to one another.</li>
</ul>
<p>Related posts:<ol>
<li><a href='http://www.geekgirls.com/2011/09/databases-from-scratch-ii-simple-database-design/' rel='bookmark' title='Databases from scratch II: Simple Database Design'>Databases from scratch II: Simple Database Design</a></li>
<li><a href='http://www.geekgirls.com/2011/09/databases-from-scratch-i-introduction/' rel='bookmark' title='Databases from scratch I: Introduction'>Databases from scratch I: Introduction</a></li>
<li><a href='http://www.geekgirls.com/2010/03/databasics3-data-entry-design/' rel='bookmark' title='Databasics III: Data entry design'>Databasics III: Data entry design</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekgirls.com/2011/09/databases-from-scratch-iii-relational-design-process/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Databases from scratch II: Simple Database Design</title>
		<link>http://www.geekgirls.com/2011/09/databases-from-scratch-ii-simple-database-design/</link>
		<comments>http://www.geekgirls.com/2011/09/databases-from-scratch-ii-simple-database-design/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 22:07:01 +0000</pubDate>
		<dc:creator>rosevines</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Office Apps]]></category>
		<category><![CDATA[database design]]></category>
		<category><![CDATA[dbms]]></category>
		<category><![CDATA[microsoft access]]></category>

		<guid isPermaLink="false">http://www.geekgirls.com/?p=1328</guid>
		<description><![CDATA[The second in a series of articles on choosing, designing and using a database program. This one covers simple table design.]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap">H</span><!--/.dropcap-->ave you ever noticed advertisements in the job classifieds for &#8216;database architects&#8217; or &#8216;database administrators&#8217;? It makes database design sound pretty intimidating, doesn&#8217;t it? If they&#8217;re so difficult to use that they require architects and administrators, what hope do we mere mortals have?</p>
<p>In fact, databases range from being ultra-simple to use to ultra-complex. In the world of personal computers, there are two main types of database programs. At the simple end of the scale are flat-file databases, also called single-file or list managers. These programs are as easy to learn as a word processor or spreadsheets. The initial concepts may take a little more time to absorb than word processing, but they&#8217;re well within anyone&#8217;s grasp. If you&#8217;re using a database at home, in a class at school or in a small club or organisation, chances are the simple flat-file database will fill your needs.</p>
<h2>Relational databases</h2>
<p>Things can get more complex when you use the other type of personal computer database program, called a relational database. With a relational database program you can create a range of databases, from flat-file structures to demanding multi-file systems. If you&#8217;re using a database in your small business, a large organisation, or an ambitious school project, you&#8217;re likely to need at least some of the features of a more complex relational database.</p>
<p>Whichever type of database program you use, the most crucial step in using it is to design your database structure carefully. <strong>The way you structure your data will affect every other action.</strong> It will determine how easy it is to enter information into the database; how well the database will trap inconsistencies and exclude duplicate records; and how flexibly you will be able to get information out of the database.</p>
<h2>A simple example</h2>
<p>Let&#8217;s take an ultra simple example: the phonebook. Say you&#8217;ve been given the job of placing your school or organisation&#8217;s phone directory on computer.</p>
<p>It should be easy: all you need is the name, the address and the phone number of each person. Your initial table design thus consists of three fields: name, address and phone number. Right?</p>
<p>Let&#8217;s check.</p>
<h2>Testing your design</h2>
<p>The way to see if your database design works is to test it with some sample data, so feed the following records into your hypothetical table:</p>
<p>J. T. Apples, 100 Megalong Dr, Haberfield, 4992122<br />
B. York, 2/53 Alice St, Leichhardt, 5050011<br />
M. R. Sullivan, 9 Jay St, Leichhardt, 4893892<br />
B. J. Anderson, 71 Wally Rd, Glebe, 2298310</p>
<p>Now tell the database program to sort the information:</p>
<p>B. J. Anderson, 71 Wally Rd, Glebe, 2298310<br />
B. York, 2/53 Alice St, Leichhardt, 5050011<br />
J. T. Apples, 100 Megalong Dr, Haberfield, 4992122<br />
M. R. Sullivan, 9 Jay St, Leichhardt, 4893892</p>
<h2>Revising your design</h2>
<p>Immediately, you can see this is not what you want. You want the table sorted alphabetically by last name, not initials.</p>
<p>How can you fix this? Well, you could do some complex manipulation using a database feature called &#8216;string functions&#8217; – <em>if</em> your program supports such a feature. Or, you could come up with a better table design in the first place: last name, initials, address and phone number. Feed your test data into this revised structure then tell the database program to sort your table using the last name followed by the initials. This time, you&#8217;ll get the correct alphabetical listing:</p>
<p>Anderson, B. J., 71 Wally Rd, Glebe, 2298310<br />
Apples, J. T., 100 Megalong Dr, Haberfield, 4992122<br />
Sullivan, M. R., 9 Jay St, Leichhardt, 4893892<br />
York, B., 2/53 Alice St, Leichhardt, 5050011</p>
<h2>Keep on refining</h2>
<p>Don&#8217;t stop there. The table can be even more effective if you break the structure down further. For instance, if you&#8217;d like to have an easy way to list only those people who live in Leichhardt, this design won&#8217;t help you. But with a little more work, you can break your database structure down further into last name, initials, street address, suburb, and phone number.</p>
<p>With this structure, you&#8217;ll be able to sort your database alphabetically by last name or by suburb, and you&#8217;ll be able to pluck out all those people who live in a particular suburb.</p>
<div class="woo-sc-box normal   "><strong>A visual example</strong></p>
<p>Here&#8217;s another example of this process in action, starting with a badly designed table structure:</p>
<div id="attachment_1333" class="wp-caption alignnone" style="width: 532px"><a href="http://www.geekgirls.com/wp-content/uploads/flawed_table.gif"><img class="size-full wp-image-1333 " title="A flawed table design" src="http://www.geekgirls.com/wp-content/uploads/flawed_table.gif" alt="A flawed table design" width="522" height="158" /></a><p class="wp-caption-text">Figure 1: A simple – and flawed – students table</p></div>
<p>This fragment of a students table suffers from various defects. The combined name field makes it hard for us to locate students by using their last names, and also difficult to sort the table alphabetically by last name. Even worse, how do we uniquely identify each student? Currently, this table uses a combination of the Name and Date of Birth fields to identify each student. What if we happen to have two Jim Smiths, both born on 5th September 1982? It’s unlikely, yes, but not impossible.</p>
<p>The composite address field is also a problem: for instance, how do we pre-sort mail by postal code so we can do bulk mailouts at discount rates?</p>
<p>By breaking the fields down we end up with double the number of fields but a much simpler and more effective structure. The new Student ID field lets us uniquely identify any student, even when they share names and birthdates. We’ve split the Names field into first and last names so we can get an alphabetical listing. We’ve done the same to the address field, allowing us to sort by postal code or suburb.</p>
<div id="attachment_1334" class="wp-caption alignnone" style="width: 591px"><a href="http://www.geekgirls.com/wp-content/uploads/improved_table.gif"><img class="size-full wp-image-1334  " title="Improved table design" src="http://www.geekgirls.com/wp-content/uploads/improved_table.gif" alt="Improved table design" width="581" height="130" /></a><p class="wp-caption-text">Figure 2: An improved students table</p></div>
</div>
<h2>More is less</h2>
<p>Notice how creating an efficient table structure consists of <em>breaking down</em> your fields into simpler and simpler components? You end up with a table with many more fields than you might originally have thought necessary, but each of those fields houses much more basic information.</p>
<p>There&#8217;s a technical term for this process: normalisation. If you wanted to become a database architect you&#8217;d have to become conversant with <em>normalisation</em> and <em>functional dependencies</em> and <em>normal forms</em>. If you&#8217;re happy to remain a lesser mortal, you can safely ignore these terms, provided you keep in mind that your task is to create a database structure that provides an efficient store for your information and that makes it flexible and easy to extract useful information.</p>
<p>In some ways, creating a database that&#8217;s effective and simple to use is almost an anti-intuitive process. For example, our initial structure for the phone directory:</p>
<p>name<br />
address<br />
phone number</p>
<p>seems like it&#8217;s a simpler design than our end result:</p>
<p>first name<br />
last name<br />
street address<br />
suburb<br />
phone number</p>
<h2>Creating useful fields</h2>
<p>What you need to remember is that while the structure might look more complex, the contents of each field have been reduced to the simplest useful components. I say &#8220;useful&#8221; because we could, of course, break each field down further. For instance, we could break the street address field into two fields, street number and street name. But what would be the point? There&#8217;s no need to extract or sort information in the database simply by street number or name, and so it&#8217;s not a <em>useful</em> basis for breaking up the field. On the other hand, if we wanted to deal with multi-line addresses which are common for businesses, such as:</p>
<p>Suite 5<br />
122 Jones Street</p>
<p>then it makes sense to break the address field down into two simpler fields, address line 1 and address line 2. You&#8217;re not likely to want to sort information based on only one of these fields, nor are you likely to use either of these fields in isolation. What <em>is</em> likely is that you&#8217;ll want to have an easy way to print address line 1 and address line 2 as separate lines when addressing envelopes. So this field division becomes useful when getting information out of your database.</p>
<h2>Permissible design infractions</h2>
<p>I should add that creating these address sub-fields is not, technically, a good solution. Notice how with this table design, many records will have nothing in the address line 2 field, as most people&#8217;s address consists of one line, rather than two. So you&#8217;ll be wasting a lot of space in your database. Additionally, what about addresses that require <em>more</em> than two lines, such as:</p>
<p>Suite 5<br />
Level III, Building A20<br />
122 Jones Street</p>
<p>Our new table structure can&#8217;t cope with this. Should we add another address line to ensure we cater to the infrequent address that needs three lines? Should we add a fourth line just in case…?</p>
<p>You can see the problems you can create by not getting the design right. A technically rigorous solution is to remove the address lines from our phonebook table altogether, and stick them in an address table, that we then link to the phonebook table by a common field.</p>
<p>However, there&#8217;s no need to fuss too much about such details. Many databases get by with minor infractions of database design rules, and you shouldn&#8217;t feel hampered by such rules provided your table structures:</p>
<ul>
<li>provide flexible and simple output (whether you&#8217;re asking an online query of the database or printing reports)</li>
<li>eliminate redundant or duplicated information</li>
<li>exclude inconsistencies</li>
</ul>
<h2>Computer-less design</h2>
<p>One thing I hope you&#8217;ve noticed is that we&#8217;ve done all our design <em>without the aid of a computer</em>. This is as it should be: it lets you focus on the significance of the task without the distractions of trying to learn a database program at the same time.</p>
<p>You can design <em>and</em> test your database structure without going near a computer. The only thing you really need to know is the type of database program you&#8217;ll use: if it&#8217;s a flat-file database, such as Microsoft Works, you&#8217;ll be limited to single-table database design. If it&#8217;s a relational program, such as FileMaker Pro, Microsoft Access or Alpha Five, you can design single- or multi-table databases.</p>
<p>In the next article in this series, we&#8217;ll move on to relational design. You&#8217;ve seen how breaking down your fields into simpler components in a single table can help make it easier to get useful information out of your database. When we look at relational design, you&#8217;ll discover how extending this process lets you address the other two goals of database design: eliminating redundant information and excluding inconsistencies.</p>
<p>Related posts:<ol>
<li><a href='http://www.geekgirls.com/2011/09/databases-from-scratch-iii-relational-design-process/' rel='bookmark' title='Databases from scratch III: The Design Process'>Databases from scratch III: The Design Process</a></li>
<li><a href='http://www.geekgirls.com/2011/09/databases-from-scratch-i-introduction/' rel='bookmark' title='Databases from scratch I: Introduction'>Databases from scratch I: Introduction</a></li>
<li><a href='http://www.geekgirls.com/2011/09/which-database-series-should-i-read/' rel='bookmark' title='Which database series should I read?'>Which database series should I read?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekgirls.com/2011/09/databases-from-scratch-ii-simple-database-design/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

