17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8: Being Normal: Normalization and Other Basic Design Issues<br />

256<br />

Figure 8-28<br />

You may not have the exact same positions for your table, but the contents should be the same. We’re now<br />

ready to start adding relationships, but we probably ought to stop and think about what kind of relationships<br />

we need.<br />

All the relationships that we’ll draw with the relationship lines in our <strong>SQL</strong> <strong>Server</strong> diagram tool are going<br />

to be one-to-zero, one, or many relationships. <strong>SQL</strong> <strong>Server</strong> doesn’t really know how to do any other kind<br />

of relationship implicitly. As we discussed earlier in the chapter, you can add things such as unique constraints<br />

and triggers to augment what <strong>SQL</strong> <strong>Server</strong> will do naturally with relations, but, assuming you<br />

don’t do any of that, you’re going to wind up with a one-to-zero, one, or many relationship.<br />

The bright side is that this is by far the most common kind of relationship out there. In short, don’t<br />

sweat it that <strong>SQL</strong> <strong>Server</strong> doesn’t cover every base here. The standard foreign key constraint (which is<br />

essentially what your reference line represents) fits the bill for most things that you need to do, and the<br />

rest can usually be simulated via some other means.<br />

We’re going to start with the central table in our system — the Orders table. First, we’ll look at any relationships<br />

that it may need. In this case, we have one — it needs to reference the Customers table. This is<br />

going to be a one-to-many relationship with Customers as the parent (the one) and Orders as the child<br />

(the many) table.<br />

To build the relationship (and a foreign key constraint to serve as the foundation for that relationship),<br />

we’re going to simply click and hold in the leftmost column of the Customers table (in the gray area) right<br />

where the CustomerNo column is. We’ll then drag to the same position (the gray area) next to the<br />

CustomerNo column in the Orders table and let go of the mouse button. <strong>SQL</strong> <strong>Server</strong> promptly pops<br />

up with the first of two dialogs to confirm the configuration of this relationship. The first, shown in<br />

Figure 8-29, confirms which columns actually relate.<br />

As I pointed out earlier in the chapter, don’t sweat it if the names that come up don’t match with what<br />

you intended — just use the combo boxes to change them back so both sides have CustomerNo in them.<br />

Note also that the names don’t have to be the same — keeping them the same just helps ease confusion<br />

in situations where they really are the same.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!