Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion
internet.com

» HOME
» NEWS
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner
Online Shopping
Memory
Domain registration
Auto Insurance Quote
KVM Switches
Phone Cards
Best Price
Web Hosting Directory
Corporate Awards
Server Racks
Online Education
Free Business Cards
Memory Upgrades
Find Software




All Talk, Little Action on 'Net Neutrality Front?

Compliance Issues Still Bedevil IT

Enterprise Spending On Virtualization To Rise

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Linked Data Planet Conference & Expo

CA ERwin® Data Modeler Proven database design and modeling. Efficiently analyze, design and deploy effective database solutions. Whitepaper: Manage SQL Server Deployments
Try it free: CA ERwin® Data Modeler


Solaris 8 Migration Assistant
Rapidly move your Solaris 8 application environments to new systems running Solaris 10 with the Solaris 8 Migration Assistant. Reduce migration risk while taking advantage of increased performance, reliability and security of the latest SPARC hardware platforms and Solaris 10 OS. »

 
Sun Eco Innovation: Good for Business, Good for the Environment
A complete solution to help you optimize and refresh your datacenter while properly recycling equipment and eliminating eWaste, including money-saving promotions to lower hardware acquisition costs. »

 
Sun Eco Innovation: Power Calculators
Power consumption has increasingly become a priority in customer's minds when purchasing new systems or storage. Sun's Power Calculators provide data on power consumption of Sun products allowing IT managers to better plan the power requirements in the datacenter to achieve better energy and cost savings. »

 
Optimize the Web Tier: Consolidate to Get More Performance in Less Space and Lower Power Consumption
Expansion in the Web tier is generally accomplished by adding more servers whenever extra capacity is needed. As the pool of servers grows larger, however, the complexity of the environment can grow exponentially. »

Production Manager (hands on)
Aquent
US-MA-Cambridge

Justtechjobs.com Post A Job | Post A Resume
MySQL
August 19, 2003
Referential Integrity in MySQL
By Ian Gilfillan

This tutorial briefly explains the concepts of referential integrity, and looks at how MySQL enforces them with its definition of foreign keys. You should be using a stable version of MySQL 4 to follow all the examples, although some examples may work with earlier versions running InnoDB tables.

What is referential integrity?

Simply put, referential integrity means that when a record in a table refers to a corresponding record in another table, that corresponding record will exist. Look at the following:

customer

customer_id

name

1

Nhlanhla

2

Anton


customer_sales

transaction_id

amount

customer_id

1

23

1

2

39

3

3

81

2

There are 2 customers in the customer table, but 3 customer_id's in the customer sales table. Assuming the two tables are linked with the customer_id field, you can tell that Nhlanhla has an amount of 23, and Anton 81. However, there is no corresponding name for customer_id 3. Foreign key relationships are described as parent/child relationships (customer being the parent, and customer_sales the child), and the record is said to be orphaned when its parent is no longer in existence.

A database in this sort of condition is referred to as having poor referential integrity (there are other kinds of integrity problems too). This is not necessarily a serious problem - one of the primary systems I work uses MyISAM tables, and has loads of orphans: article blurbs and article bodies not linked to any articles, but these don't do much harm besides prickle my aesthetic sensibility, and we've never needed to fix this. However, it is not good design, and can sometimes lead to problems, so you should avoid a situation like this where possible.

In the past, the MySQL DBMS could not enforce this, and the responsibility passed to the code to do so. But this wasn't good enough for serious systems, and one of the most frequently requested features in later versions of MySQL was that of foreign keys, enabling MySQL data to maintain referential integrity. A foreign key is simply a field in one table that corresponds to a primary key in another table. In the example above, customer_id would be the primary key in the customer table, uniquely identifying each record, and transaction_id would be the same in the customer_sales table. In the customer_sales table, the customer_id field could be an example of a foreign key, referring to its namesake in the customer table. A transaction should not exist without an associated customer. The code that generated these tables is clearly buggy!

Defining Foreign Keys in MySQL

Strictly speaking, for a field to be a foreign key, it needs to be defined as such in the database definition. You can 'define' a foreign key in any MySQL table type (including the default MyISAM table type), but they do not actually do anything - they are only used to enforce referential integrity in InnoDB tables.

In order to create a foreign key, you need the following:

  • Both tables need to be InnoDB tables.
  • To use the syntax FOREIGN KEY(fk_fieldname) REFERENCES table_name (fieldname)
  • The field being declared a foreign key needs to be declared as an index in the table definition

Here is how you would define the two tables above with a foreign key:

CREATE TABLE customer 
(
    customer_id INT NOT NULL,
    name        VARCHAR(30),
    PRIMARY KEY (customer_id)
) TYPE = INNODB;

CREATE TABLE customer_sales 
(
    transaction_id INT NOT NULL,
    amount 	   INT,
    customer_id    INT NOT NULL,
    PRIMARY KEY(transaction_id),
    INDEX (customer_id),
    FOREIGN KEY (customer_id) REFERENCES customer (customer_id) 
) TYPE = INNODB;

If you get the rather unhelpful error message:

ERROR 1005: Can't create table './test/customer_sales.frm' (errno: 150)

then check your foreign key definitions carefully - something is wrong with the definition. Common causes are a table not being of type InnoDB, a missing index on the same field (customer_id), or attempting to set a field to NULL when it cannot be (see the ON DELETE SET NULL clause below).

Referential integrity can be compromised in three situations: when creating a new record, deleting a record or updating a record. The FOREIGN KEY (transaction_id) REFERENCES customer (customer_id) clause ensures that when a new record is created in the customer_sales table, it must have a corresponding record in the customer table. After creating the above tables, insert the following data, which we will use to demonstrate some of the concepts:

mysql> INSERT INTO customer VALUES(1,'Nhlanhla'),(2,'Anton');
Query OK, 2 rows affected (0.00 sec)
mysql> INSERT INTO customer_sales VALUES(1,23,1),(3,81,2);
Query OK, 2 rows affected (0.00 sec)

Now insert the third record, referring to the non-existent customer 3:

mysql> INSERT INTO customer_sales VALUES(2,39,3);
ERROR 1216: Cannot add or update a child row: a foreign key constraint fails

You cannot add the record, as customer_id 3 does not exist. The constraint has ensured your data keeps its integrity! However, what happens when we delete a record? Let's add a customer 3, then add the customer_sales record again, after which we delete the 3rd customer:

mysql> INSERT INTO customer VALUES(3,'Malvin');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO customer_sales VALUES(2,39,3);
Query OK, 1 row affected (0.01 sec)
mysql> DELETE FROM customer WHERE customer_id=3;
ERROR 1217: Cannot delete or update a parent row: a foreign key constraint fails

So the constraint holds, and we would need to first delete the record from the customer_sales table. There is a way we could have allowed the delete to go ahead, which we will look at shortly, but first we will need to drop and recreate the index.

Go to page: 1  2  Next  

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MySQL Archives

Best Practices: Make the Case for IT Investments. Complimentary Independent Report. Download Now!
Five Trends for Application Development. Download Your Complimentary Report. Exclusive. Act Now.
Webcast: Five Virtualization Trends to Watch. Produced for HP, Citrix, and Intel.
Flash Demo: Learn how IBM Information Server Blade is easy to manage, highly scalable and efficient.
Download: SQL Backup & DBA Best Practices eBook.


Latest Forum Threads
MySQL Forum
Topic By Replies Updated
Transfer information from an access database to MySQl database sculptor44 2 May 14th, 09:50 PM
MERGE STORAGE ENGINE Vs UNION ALL dbnewbie 0 May 14th, 12:16 AM
database sculptor44 0 May 5th, 09:10 PM
arrangeable Database sculptor44 0 May 5th, 08:53 PM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES