Home > Articles > Data > SQL Server

SQL Server Reference Guide

Hosted by

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Database Objects: Table Relationships

Last updated Mar 28, 2003.

In our last tutorial (Database Objects: Tables&), we learned that tables are columns and rows of data. In any Relational Database Management System (RDBMS), the tables are atomic – that is, they only store the data that is pertinent to the table. Recall that I began the discussion with a selection from two tables, called authors and titles from the pubs database. We'll use those tables again.

In the earlier lesson, I explained the basics of relating the seemingly unconnected tables. I'll continue that line of thought in this article.

I also introduced the concept of relational algebra. Let's take a look at that concept again, and also combine a little relational calculus as well.

You may remember from school that relational algebra deals with sets of numbers, and the way they do or don't join. If you recall, each set (sometimes called a relation) contains values, and those values are called domains. That holds true in SQL databases as well. The other concept that holds true for both SQL Server and relational algebra is the way the sets of data join. Here are a few of those concepts:

The first thing to remember from relational algebra is that the results from any join form another set. In SQL, the results of any join end up as another data-set, in a table-like structure. This idea of creating smaller (or larger) results from a select statement will help you immensely as we move forward.

A select is the act of pulling out certain values from a set of data. SQL implements this concept with the SELECT statement, which you'll use quite often. Select statements are across rows or records of data and result in another data-set. We'll see how to fully form a SQL SELECT statement in a bit.

A union combines the results of two or more sets of data. The results of a union always create a larger data set. SQL Server also supports a UNION qualifier on a SELECT statement.

An intersection of data results in a listing of the common values of two or more sets. This is the most common type of operation that you'll use in SQL, since you will often want to know things like "which books were written by which authors?" We'll use this concept to build our tables, and also to build lots of queries as we move on.

A difference is the operation that shows the items in one set that do not exist in the others. SQL Server supports this type of operation by several methods, which we'll see a bit later.

Now we move to the relational calculus operations. Unless you had to suffer through a math discipline in your college degree as I did, you may not have been exposed to these concepts yet by these names, but I'll bet you'll understand them intuitively. They are:

Exists: The exists clause results in a true or false state when a value is in a set of data. SQL will perform the action requested when an EXISTS clause is used and the data is located in a SELECT statement. The action won't be implemented when the value isn't in the set. There is an opposite of this clause in SQL, called NOT EXISTS.

In: The in clause is similar to the exists clause, but isn't used to test for a true or false condition, but merely to include values in the action requested. There is an opposite of this clause in SQL, called NOT IN.

Between: The between clause is very useful and does pretty much what its name implies; it tests for true and false by including the values between two conditions. The immediate question is "inclusive or not?" and the answer is inclusive. If you're looking for a non-inclusive set, then you would use the > and < symbols. There is an opposite of this clause in SQL, called NOT BETWEEN.

As you can see, we have some powerful tools to work with. To make them even more powerful, you can combine these statements to create some fairly complex conditions. (We'll do that in future tutorials.) For now, the important thing is to have the ideas well in hand.

So far, we've only seen the way we select data from sets. We still need to cover the relationships of those sets.

Relating tables

To begin our discussion of table relationships, we need to define the various relationship types. We'll follow these concepts up with implementations as we design our databases.

The first type of relationship between tables isn't between two tables at all – it's the self-relationship. It's important to keep in mind that a single table really isn't single – in concept, anyway. It's an un-tapped source of data sets. There are situations in which you have a table with data that relates to other data in the table. For instance, an employee table has a domain (there's that algebra again!) which would contain all employees. Everyone who receives a paycheck would be considered an employee.

How would we handle defining each employee's manager? We might have another table for that, but it's unnecessary to go to all that trouble. We can include a reference to the employee number to whom a certain employee reports, right in the same table. We can then use SQL statements to relate the information right back to another row in the same table.

The next type of join is the one-to-one join. This condition exists if a slice of data in one set has one and only one match in another set. This type is actually pretty rare, sometimes seen in order-fulfillment systems.

The next type of join is the one-to-many relationship. This type of relationship is the most common. It states that there are many items in one or more sets that relate to another. For instance, one manager may manage many employees, but normally one employee doesn't have many managers! OK, bad example...

We saw the next condition in the last tutorial: the many-to-many relationship. In this type, two or more sets of data are related to each other more than once on both sides. For instance, many authors might work on a single book, and many books might be written by a single author.

Now that you have these concepts in hand, you can take real-world examples of data sets from your work and plug them into the join and selection types described above. It's helpful to ask these questions as you move forward on your design:

  • How does the process in question work? For instance, you might say "A seller sells items from inventory." Right away you see several sets of data (sellers, buyers, sales, items, inventory, etc.) and relationships (sales to items, items to inventory, sellers to buyers, etc.). These define your tables.

  • In how many ways are each set related to each other? There are really two questions here. How are the sets related, and how many times each? This is your relationship design phase. I'll show you a handy tool to discover the answers, the Entity Relationship Diagram (ERD), in another tutorial.

  • What is required? You need a seller to sell, a buyer to buy, and items in inventory to sell. The answers here determine the NULL conditions and also helps define the keys for your constraints.

What makes each set unique? We'll deal with this question more in the next tutorial where I'll describe keys more fully.

These are the basic concepts of the relationship puzzle. In future articles, I'm going to springboard this base knowledge into logical and physical database design, keys, select logic, capacity and performance planning and more. You can see these are important base concepts, which we'll build on for the rest of our programming series. Keep this reference handy!