O'Reilly Databases

oreilly.comSafari Books Online.Conferences.

We've expanded our coverage and improved our search! Search for all things Database across O'Reilly!

Search Search Tips

advertisement
AddThis Social Bookmark Button

Listen Print Subscribe to Databases Subscribe to Newsletters
aboutSQL

Good Relations

03/13/2001

Last week, we started working with a fundamental aspect of database structure -- database keys. This week, we'll take a look at how keys are used to construct relationships between different tables in a database. This is the core function of relational databases. You may have a rocky relationship with your database, but after this week, your data should relate just fine!

Your first relationship

There is nothing magical, complex, or inherently hard about data relationships. In a nutshell, the most simple database relationship is formed by taking the key field value of a record in one table and inserting it into an identical field in the related record in a second table. We'll work through an example from the beginning.

In this scenario, you're building a database for your music collection. You may have started with a single database table that looks something like this:

CDID Artist Title Year Quality
15 Peter Gabriel So 1984 10

You've got a single table with a primary-key value called CDID that contains information about a single CD in your collection. But if you have an extensive collection of Peter Gabriel albums, you'd end up with a lot of redundancy in your database.

Also in aboutSQL:

SQL Data Types

Working With Tables

Data Definition Language

Introducing SQL Sets

SQL Subqueries

CDID Artist Title Year Quality
15 Peter Gabriel So 1984 10
16 Peter Gabriel Us 1992 10

You can imagine the progression from here, especially for a large database of music like an e-commerce site. This brings us to our first SQL Wisdom in a long time:

SQL Wisdom #5: Data redundancy is generally a result of bad design.

Any time you have to re-enter data, you're likely to enter it incorrectly. In addition, redundant data takes up extra space in the database. There are cases where this is a good idea, typically for specific performance reasons; but in general, you can assume that you need to redesign your database if you see a lot of redundant data. And that's the whole essence of relational databases.

Relationships are all about reducing database redundancy. We can take the record for Peter Gabriel and break it into a separate table.

ArtistID ArtistName
22 Peter Gabriel

Now that we have the information about the artist separated from the album information, we can redesign the table of information for the CDs themselves.

CDID ArtistID Title Year Quality
15 22 So 1984 10
16 22 Us 1992 10

I've highlighted the column where the ArtistName string was replaced with a primary key from the table containing the related data record. In database parlance, this is typically called a foreign key. The two database tables are now related by the ArtistID key field.

Long-term relationships

The database relationship we created in the previous section is simple, but it is an example of the most common type of database relationship. There are three standard types of database relationships:

  • One-to-one relationships link a single record in one table to 0 or 1 records in a related table.
  • One-to-many relationships link a single record in one table to 0, 1, or more records in a related table.
  • Many-to-many relationships link multiple records in one table to 0, 1, or more records in another table through an intermediate linking table.

The example shows a one-to-many relationship -- the most common type of database relationship. The Artist table contains a single "Peter Gabriel" record which can have 0, 1, or more related records in the CDs table.

Before we move on to the details of different types of relationships, I want to point out a few features of all relationships:

  • Any fixed-size field can be used as a key to join fields, but try to use the smallest integer types or a short character string.
  • Numbers are always better than text.
  • Any single field can be used as a key in n other tables.
  • Fields that are used for relationships should typically be indexed for performance.

There are many other details to discuss -- referential integrity, cascaded DELETEs, how NULLs figure into relationships, and a host of other topics that we'll discuss as we move forward with aboutSQL.

Next steps

Now that we've got the basics of keys and relationships, we'll spend next week discussing one-to-one and many-to-many relationships. After that, we'll move back to SQL proper and cover using the JOIN keyword to create SQL statements that use database relationships. Until then, feel free to send me your comments, questions, and feedback.

John Paul Ashenfelter is president and CTO of TransitionPoint.com, a technology development and analysis group focusing on web database applications.

Read more aboutSQL columns.


Return to ONLamp.com.




Tagged Articles

Post to del.icio.us

This article has been tagged:

mysql

Articles that share the tag mysql:

MySQL FULLTEXT Searching (155 tags)

Live Backups of MySQL Using Replication (152 tags)

Advanced MySQL Replication Techniques (125 tags)

Ten MySQL Best Practices (59 tags)

Rolling with Ruby on Rails (56 tags)

View All

programming

Articles that share the tag programming:

Rolling with Ruby on Rails (1374 tags)

Very Dynamic Web Interfaces (279 tags)

Ajax on Rails (231 tags)

Understanding MVC in PHP (202 tags)

A Simpler Ajax Path (186 tags)

View All

sql

Articles that share the tag sql:

How to Misuse SQL's FROM Clause (74 tags)

Configuring Database Access in Eclipse 3.0 with SQLExplorer (28 tags)

Managing Many-to-Many Relationships with PL/pgSQL (18 tags)

Hierarchical SQL (16 tags)

The Outer Limits of SQL JOINs (14 tags)

View All

Sponsored Resources

  • Inside Lightroom

Related to this Article

Understanding Oracle Clinical Understanding Oracle Clinical
by Joan M. Johnson
May 2007
$9.99 USD

Inside SQLite Inside SQLite
by Sibsankar Haldar
April 2007
$9.99 USD

Advertisement
O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com