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.

❑ Date of hire<br />

❑ Date terminated (if there is one)<br />

❑ The employee’s manager<br />

❑ Department<br />

Start by trying to figure out a layout for yourself.<br />

Before we start looking at this together, let me tell you not to worry too much if your layout isn’t exactly<br />

like mine. There are as many database designs as there are database designers — and that all begins with<br />

table design. We all can have different solutions to the same problem. What you want to look for is<br />

whether you have all the concepts that need to be addressed. That being said, let’s take a look at one<br />

way to build this table.<br />

We have a special column here. The EmployeeID is to be generated by the system and therefore is an<br />

excellent candidate for either an identity column or a ROWGUIDCOL. There are several reasons you might<br />

want to go one way or the other between these two, but we’ll go with an identity column for a couple of<br />

reasons:<br />

❑ It’s going to be used by an average person. (Would you want to have to remember a GUID?)<br />

❑ It incurs lower overhead.<br />

We’re now ready to start constructing our script:<br />

CREATE TABLE Employees<br />

(<br />

EmployeeID int IDENTITY NOT NULL,<br />

Chapter 5: Creating and Altering Tables<br />

For this column, the NOT NULL option has essentially been chosen for us by virtue of our use of an<br />

IDENTITY column. You cannot allow NULL values in an IDENTITY column. Note that, depending on our<br />

server settings, we will, most likely, still need to include our NOT NULL option (if we leave it to the<br />

default we may get an error depending on whether the default allows NULLs).<br />

Next up, we want to add in our name columns. I usually allow approximately 25 characters for names.<br />

Most names are far shorter than that, but I’ve bumped into enough that were rather lengthy (especially<br />

since hyphenated names have become so popular) that I allow for the extra room. In addition, I make<br />

use of a variable-length data type for two reasons:<br />

❑ To recapture the space of a column that is defined somewhat longer than the actual data usually<br />

is (retrieve blank space)<br />

❑ To simplify searches in the WHERE clause — fixed-length columns are padded with spaces,<br />

which requires extra planning when performing comparisons against fields of this type<br />

The exception in this case is the middle initial. Since we really need to allow for only one character here,<br />

recapture of space is not an issue. Indeed, a variable-length data type would actually use more space in<br />

this case, since a varchar needs not only the space to store the data, but also a small amount of overhead<br />

space to keep track of how long the data is. In addition, ease of search is not an issue since, if we<br />

have any value in the field at all, there isn’t enough room left for padded spaces.<br />

133

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

Saved successfully!

Ooh no, something went wrong!