<?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>Team Tutorials &#187; database</title>
	<atom:link href="http://teamtutorials.com/tag/database/feed" rel="self" type="application/rss+xml" />
	<link>http://teamtutorials.com</link>
	<description></description>
	<lastBuildDate>Thu, 05 Apr 2012 19:38:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Referential Integrity with MySQL</title>
		<link>http://teamtutorials.com/database-tutorials/referential-integrity-with-mysql?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=referential-integrity-with-mysql</link>
		<comments>http://teamtutorials.com/database-tutorials/referential-integrity-with-mysql#comments</comments>
		<pubDate>Fri, 04 Dec 2009 03:28:11 +0000</pubDate>
		<dc:creator>Mike Maguire</dc:creator>
				<category><![CDATA[Database Tutorials]]></category>
		<category><![CDATA[MySQL Tutorials]]></category>
		<category><![CDATA[auto increment]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Heidi]]></category>
		<category><![CDATA[insertion]]></category>
		<category><![CDATA[integrity]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[relationship]]></category>

		<guid isPermaLink="false">http://teamtutorials.com/?p=2503</guid>
		<description><![CDATA[Referential Integrity is when the rows from one table are “cross-referenced” and verified with those in another table. This tutorial will walk you through using MySQL to enforce this rule.]]></description>
			<content:encoded><![CDATA[<p>Referential Integrity is when the rows from one table are “cross-referenced” and verified with those in another table. This tutorial will walk you through using MySQL to enforce this rule. For example, let’s say you want to have a customer table that contains all the information about your customers and then you want to have an invoice table that will keep the information for invoices. This invoice table will need to be associated to the customer table through the customer id. This relationship would allow you to get all invoices for a customer by searching for their ID. Enforcing referential integrity also will prevent the insertion of a invoice with a customer ID that isn’t in the customer table. Let’s get started.</p>
<p>First, we need to decide what our tables are going to look like. These could vary based on the application you are working with but for this tutorial, we will keep them basic. First, the customer table which doesn’t need to be anything special except ensuring that the type is INNODB and that you set the primary key. </p>
<pre class="brush: sql; title: Code Snippet:; notranslate">
CREATE TABLE customers
(
   customer_id INT NOT NULL AUTO_INCREMENT,
   name 			VARCHAR(50),
	address  	VARCHAR(60),
	city 			VARCHAR(45),
	state			VARCHAR(2),
	zip 			INT,
   PRIMARY KEY (customer_id)
) TYPE = INNODB;
</pre>
<p>This statement will create a table called customers that contains customer_id, name, address, city, state, and zip columns. It sets the customer id field to the primary key and makes it auto increment for each insert. Next we need to build the invoice table that will house the information about an invoice. (*Note: Normally an invoice would reference an item table to house the items for each invoice, but for sake of remaining simple, we will just have an items field that contains a varchar with comma separated items)</p>
<pre class="brush: sql; title: Code Snippet:; notranslate">
CREATE TABLE invoice
(
   invoice_id		INT NOT NULL AUTO_INCREMENT,
   customer_id	   INT,
   submit_date    TIMESTAMP NOT NULL,
	total				DECIMAL(10,2),
	tax				DECIMAL(10,2),
	shipping			DECIMAL(10,2),
	items				VARCHAR(100),
   PRIMARY KEY(invoice_id),
   INDEX (customer_id),
   FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
) TYPE = INNODB;
</pre>
<p>This statement will create the invoice table containing invoice id, customer id, submit_date, total, tax, shipping, and items, columns. It sets the primary key to the invoice_id. INDEX tells it to index that column so that when we search the DB for that value it will be faster to return the results. Then the foreign key sets the customer_id of this table to look at the customer_id of the customer table to make sure it exists. </p>
<p>Let’s go ahead and make sure everything works as it is supposed to. First, we will try to insert an invoice for a customer ID that we haven’t inserted yet. Use the following insert statement.</p>
<pre class="brush: sql; title: Code Snippet:; notranslate">
 insert into invoice (customer_id,submit_date,total,tax,shipping,items) values (1,current_timestamp,54.22,4.50,10.25,'RAM, Power Supply, Case');
</pre>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/12/referential_integrity_with_mysql_01.jpg" alt="referential_integrity_with_mysql_01" title="referential_integrity_with_mysql_01" width="675" height="122" class="alignnone size-full wp-image-2506" /></p>
<p>You should receive an error similar to the above picture. This just means that there is no customer with that ID so you can’t insert an invoice for them. Next, lets add the customer to the customer table.</p>
<pre class="brush: sql; title: Code Snippet:; notranslate">
insert into customers (name,address,city,state,zip) values ('Mike','100 That One Place','Anywhere','TX',22222);
</pre>
<p>Once you insert that row you should be able to successfully insert the invoice without any issues. The final thing that will referential integrity will enforce is to ensure you can’t delete customers that have invoice attached to them. Let’s try to ensure it works.</p>
<pre class="brush: sql; title: Code Snippet:; notranslate">
delete from customers where customer_id = 1;
</pre>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/12/referential_integrity_with_mysql_02.jpg" alt="referential_integrity_with_mysql_02" title="referential_integrity_with_mysql_02" width="698" height="122" class="alignnone size-full wp-image-2507" /></p>
<p>You should receive the error above. If you delete the invoice from the invoice table first, you will then be able to delete the customer. That concludes this intro to referential integrity. I hope this is clear to you and thanks for reading.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://teamtutorials.com/windows-tutorials/downloading-and-installing-heidi-sql" title="Downloading and Installing Heidi SQL">Downloading and Installing Heidi SQL</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/access-a-mysql-database-using-pdo" title="Access a MySQL Database Using PDO">Access a MySQL Database Using PDO</a></li><li><a href="http://teamtutorials.com/off-topic/launch-of-wootstat-the-top-woot-tracker-on-facebook" title="Launch of WootStat &#8211; The Best Woot Tracker on Facebook">Launch of WootStat &#8211; The Best Woot Tracker on Facebook</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/how-to-return-mysql-results-to-a-table" title="How to Return MySQL Results to a Table">How to Return MySQL Results to a Table</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/editing-mysql-data-using-php" title="Editing MySQL Data Using PHP">Editing MySQL Data Using PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/database-tutorials/referential-integrity-with-mysql/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Launch of WootStat &#8211; The Best Woot Tracker on Facebook</title>
		<link>http://teamtutorials.com/off-topic/launch-of-wootstat-the-top-woot-tracker-on-facebook?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=launch-of-wootstat-the-top-woot-tracker-on-facebook</link>
		<comments>http://teamtutorials.com/off-topic/launch-of-wootstat-the-top-woot-tracker-on-facebook#comments</comments>
		<pubDate>Mon, 06 Apr 2009 23:36:30 +0000</pubDate>
		<dc:creator>John Ward</dc:creator>
				<category><![CDATA[Off Topic]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[scraping]]></category>
		<category><![CDATA[woot]]></category>

		<guid isPermaLink="false">http://teamtutorials.com/?p=1813</guid>
		<description><![CDATA[Introducing WootStat, the premier <a href="http://www.wootstat.com">Woot tracker for Facebook</a>. Mike and myself decided to work with the Facebook API. We ended up created a <a href="http://www.facebook.com/apps/application.php?id=33428063902">cool application for tracking items on Woot.com</a>. After checking out the competiton we stack up pretty well. I think we have the most features of any WootTracker on Facebook. ]]></description>
			<content:encoded><![CDATA[<p><!--noadsense--><br />
Introducing WootStat, the premier <a href="http://www.wootstat.com">Woot tracker for Facebook</a>. Mike and myself decided to work with the Facebook API. We ended up created a <a href="http://www.facebook.com/apps/application.php?id=33428063902">cool application for tracking items on Woot.com</a>. After checking out the competiton we stack up pretty well. I think we have the most features of any WootTracker on Facebook. </p>
<p><a href="http://www.facebook.com/apps/application.php?id=33428063902"><img src="http://teamtutorials.com/wp-content/uploads/2009/04/woot-stat-facebook.gif" alt="Woot Stat on Facebook" title="Woot Stat on Facebook" width="396" height="396" class="alignnone size-full wp-image-1816" /></a></p>
<p>WootStat offers:</p>
<ul>
<li>User Voting</li>
<li>Price Comparison</li>
<li>Sharing Capabilities</li>
<li>Past items</li>
<li>Woot-Off Predictions</li>
<li>Top Users</li>
<li>Best and Worst Items</li>
<li>Email Notifications</li>
<li>&#8230;and more to come</li>
</ul>
<p>The user who refers the most friends this month will <strong>win a $50 NewEgg.com Gift Card</strong>. At the time of this post the leader has only 3 referrals.<a href="http://apps.facebook.com/wootstat/addfriends.php"> All you have to do is add WootStat and then Invite Your Friends.</a></p>
<p>If you want to check out the app you can check it out at <a href="http://wootstat.com/">WootStat.com</a>.</p>
<p><a href="http://www.facebook.com/apps/application.php?id=33428063902">See the Facebook about page</a></p>
<p><a href="http://www.woot.com/WhatIsWoot.aspx">What is woot?</a></p>
<p>I hope that this would lead to some Facebook API tutorials, but I can&#8217;t promise anything. As usual if you have any question post them below or feel free to use the contact form. We generally respond to all inquires.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://teamtutorials.com/database-tutorials/referential-integrity-with-mysql" title="Referential Integrity with MySQL">Referential Integrity with MySQL</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/using-jquery-to-reorder-a-list-and-update-a-database" title="Using jQuery to Reorder a List and Update a Database">Using jQuery to Reorder a List and Update a Database</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/creating-checkboxes-based-on-sql-results" title="Creating Checkboxes Based on SQL Results">Creating Checkboxes Based on SQL Results</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/jquery-pop-over-effects" title="jQuery Pop Over Effects">jQuery Pop Over Effects</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/password-verification-and-strength-checker" title="Password Verification and Strength Checker">Password Verification and Strength Checker</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/off-topic/launch-of-wootstat-the-top-woot-tracker-on-facebook/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

