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 />

I’ve touched on this before, but you just about have to drag me kicking and screaming in order to get me<br />

to allow nulls in my databases. There are situations where you just can’t avoid it — “undefined” values<br />

are legitimate. I’ll still often fill text fields with actual text saying “Value Unknown” or something like<br />

that.<br />

The reason I do this is because nullable fields promote errors in much the same way that undeclared<br />

variables do. Whenever you run across null values in the table you wind up asking yourself, “Gee, did I<br />

mean for that to be there, or did I forget to write a value into the table for that row?” — that is, do I<br />

have a bug in my program?<br />

The next issue we faced was default values. We couldn’t have a default for OrderID because we’re making<br />

it an identity column (the two are mutually exclusive). For OrderDate, however, a default made some<br />

level of sense. If an OrderDate isn’t provided, then we’re going to assume that the order date is today.<br />

Last, but not least, is the CustomerNo — which customer would we default to? Nope — can’t do that here.<br />

Next up was the issue of an identity column. OrderID is an ideal candidate for an identity column —<br />

the value has no meaning other than keeping the rows unique. Using a counter such as an identity field<br />

gives us a nice, presentable, and orderly way to maintain that unique value. We don’t have any reason to<br />

change the identity seed and increment, so we won’t. We’ll leave it starting at one and incrementing by one.<br />

Now we’re ready to move on to our next table — the OrderDetails table as defined in Figure 8-25.<br />

Figure 8-25<br />

For this table, the OrderID column is going to have a foreign key to it, so our data type is decided for<br />

us — it must be of the same type and size as the field it’s referencing, so it’s going to be an int.<br />

The LineItem is going to start over again with each row, so we probably could have gotten as little as a<br />

tinyint here. We’re going to go with an int on this one just for safety’s sake. (I’ve had people exceed<br />

limits that have been set on this sort of thing before.)<br />

PartNo is, for this table, actually going to be defined by the fact that it needs to match up with the PartNo<br />

in the Products table. It’s going to be using a char(6) in that table (we’ll come to it shortly), so that’s<br />

what we’ll make it here.<br />

Qty is guesswork. The question is, what’s the largest order you can take as far as quantity for one lineitem<br />

goes? Since we don’t know what we’re selling, we can’t really make a guess on a maximum quantity<br />

(for example, if we were selling barrels of oil, it might be bought literally millions of barrels at a time).<br />

We’re also using an int here, but we would have needed a data type that accepted decimals if we were<br />

selling things like gallons of fuel or things by weight, so be sure to consider your needs carefully.<br />

253

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

Saved successfully!

Ooh no, something went wrong!