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

Table Constraints<br />

ON<br />

Table constraints are quite similar to column constraints, in that they place restrictions on the data that<br />

can be inserted into the table. What makes them a little different is that they may be based on more than<br />

one column.<br />

Again, I will be covering these in the constraints chapter, but examples of table-level constraints include<br />

PRIMARY and FOREIGN KEY constraints, as well as CHECK constraints.<br />

Remember when we were dealing with database creation, and we said we could create different filegroups?<br />

Well, the ON clause in a table definition is a way of specifically stating on which filegroup (and, therefore,<br />

physical device) you want the table located. You can place a given table on a specific physical device, or,<br />

as you will want to do in most cases, just leave the ON clause out, and it will be placed on whatever the<br />

default filegroup is (which will be the PRIMARY unless you’ve set it to something else). We will be looking<br />

at this usage extensively in our chapter on performance tuning.<br />

TEXTIMAGE_ON<br />

This one is basically the same as the ON clause we just looked at, except that it lets you move a very specific<br />

part of the table to yet a different filegroup. This clause is only valid if your table definition has<br />

text, ntext, or image column(s) in it. When you use the TEXTIMAGE_ON clause, you move only the<br />

BLOB information into the separate filegroup — the rest of the table stays either on the default filegroup<br />

or with the filegroup chosen in the ON clause.<br />

Creating a Table<br />

130<br />

OK, so why is a CHECK constraint a table constraint? Isn’t it a column constraint since<br />

it affects what you can place in a given column? The answer is that it’s both. If it is<br />

based on solely one column, then it meets the rules for a column constraint. If, however<br />

(as CHECK constraints can), it is dependent on multiple columns, then you have<br />

what would be referred to as a table constraint.<br />

There can be some serious performance increases to be had by splitting your database<br />

into multiple files, and then storing those files on separate physical disks.<br />

When you do this, it means you get the I/O from both drives. Major discussion of<br />

this is outside the scope of this book, but keep this in mind as something to gather<br />

more information on should you run into I/O performance issues.<br />

All right, we’ve seen plenty; we’re ready for some action, so let’s build a few tables.<br />

When we started this section, we looked at our standard CREATE syntax of:<br />

CREATE

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

Saved successfully!

Ooh no, something went wrong!