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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

240<br />

OK, we’ve got our CustomerNotes table into the database, but now we notice a problem — the way our<br />

primary key is declared, we can only have only one note per customer. More than likely, we are going to<br />

keep taking more and more notes on the customer over time. That means that we need to change our<br />

primary key, and leaves us with a couple of options depending on our requirements:<br />

❑ Make the date part of our primary key: This is problematic from two standpoints. First, we’re<br />

tracking which employee took the note — what if two different employees wanted to add notes<br />

at the same time? We could, of course, potentially address this by also adding EmployeeID to<br />

the primary key. Second, what’s to say that even the same employee wouldn’t want to enter two<br />

completely separate notes on the same day (OK, so, since this is a datetime field, they could do<br />

it as long as they didn’t get two rows inserted at the same millisecond — but just play along<br />

with me here)? Oops, now even having the EmployeeID in the key doesn’t help us.<br />

❑ Add another column to help with the key structure: We could either do this by adding a counter<br />

column for each note per customer. As yet another alternative, we could just add an identity column<br />

to ensure uniqueness — it means that our primary key doesn’t really relate to anything, but<br />

that isn’t always a big deal (though it does mean that we have one more index that has to be<br />

maintained) and it does allow us to have a relatively unlimited number of notes per customer.<br />

I’m going to take the approach of adding a column I’ll call “Sequence” to the table. By convention (it’s<br />

not a requirement and not everyone does it this way), primary keys are normally the first columns in<br />

your table. If we were going to be doing this by script ourselves, we’d probably just issue an ALTER TABLE<br />

statement and ADD the column — this would stick our new column down at the end of our column list.<br />

If we wanted to fix that, we’d have to copy all the data out to a holding table, drop any relationships to<br />

or from the old table, drop the old table, CREATE a new table that has the columns and column order we<br />

want, then re-establish the relationships and copy the data back in (a long and tedious process). With the<br />

diagramming tools, however, <strong>SQL</strong> <strong>Server</strong> takes care of all that for us.<br />

To insert a new row in the middle of everything, I just right-click on the row that is to immediately<br />

follow the row I want to insert and select Insert Column. The tool is nice enough to bump everything<br />

down for me to create space just like Figure 8-12.<br />

Figure 8-12

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

Saved successfully!

Ooh no, something went wrong!