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 5: Creating and Altering Tables<br />

There is a Windows API call to generate a GUID in normal programming, but, in addition to the ROWGUIDCOL<br />

option on a column, <strong>SQL</strong> has a special function to return a GUID — it is called the NEWID() function,<br />

and can be called at any time.<br />

COLLATE<br />

This works pretty much just as it did for the CREATE DATABASE command, with the primary difference<br />

being in terms of scope (here, we define at the column level rather than the database level).<br />

NULL/NOT NULL<br />

This one is pretty simple — it states whether the column in question accepts NULL values or not. The<br />

default, when you first install <strong>SQL</strong> <strong>Server</strong>, is to set a column to NOT NULL if you don’t specify nullability.<br />

There are, however, a very large number of different settings that can affect this default, and change its<br />

behavior. For example, setting a value by using the sp_dbcmptlevel stored procedure or setting ANSIcompliance<br />

options can change this value.<br />

Column Constraints<br />

We have a whole chapter coming up on constraints, so we won’t spend that much time on it here. Still, it<br />

seems like a good time to review the question of what column constraints are — in short, they are restrictions<br />

and rules that you place on individual columns about the data that can be inserted into that column.<br />

For example, if you have a column that’s supposed to store the month of the year, you might define that<br />

column as being of type tinyint — but that wouldn’t prevent someone from inserting the number 54 in<br />

that column. Since 54 would give us bad data (it doesn’t refer to a month), we might provide a constraint<br />

that says that data in that column must be between 1 and 12. We’ll see how to do this in our next chapter.<br />

Computed Columns<br />

128<br />

I highly recommend explicitly stating the NULL option for every column in every<br />

table you ever build. Why? As I mentioned before, there are a large number of different<br />

settings that can affect what the system uses for a default for the nullability of<br />

a column. If you rely on these defaults, then you may find later that your scripts<br />

don’t seem to work right (because you or someone else has changed a relevant setting<br />

without realizing its full effect).<br />

You can also have a column that doesn’t have any data of its own, but whose value is derived on the fly<br />

from other columns in the table. If you think about it, this may seem odd since you could just figure it<br />

out at query time, but really, this is something of a boon for many applications.<br />

For example, let’s say that we’re working on an invoicing system. We want to store information on the<br />

quantity of an item we have sold, and at what price. It used to be fairly commonplace to go ahead and<br />

add columns to store this information, along with another column that stored the extended value (price<br />

times quantity). However, that leads to unnecessary wasting of disk space and maintenance hassles<br />

associated with when the totals and the base values get out of synch with each other. With a computed<br />

column, we can get around that by defining the value of our computed column to be whatever multiplying<br />

price by quantity creates.

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

Saved successfully!

Ooh no, something went wrong!