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 6: Constraints<br />

A table can have a maximum of one primary key. As I mentioned earlier, it is rare to have a table in<br />

which you don’t want a primary key.<br />

When I say “rare” here, I mean very rare. A table that doesn’t have a primary key severely violates the<br />

concept of relational data — it means that you can’t guarantee that you can relate to a specific record.<br />

The data in your table no longer has anything that gives it distinction.<br />

Situations where you can have multiple rows that are logically identical are actually not that uncommon,<br />

but that doesn’t mean that you don’t want a primary key. In these instances, you’ll want to take a look<br />

at fabricating some sort of key. This approach has most often been implemented using an identity column,<br />

although using a GUID is now an appropriate alternative in some situations.<br />

A primary key ensures uniqueness within the columns declared as being part of that primary key, and<br />

that unique value serves as an identifier for each row in that table. How do we create a primary key?<br />

Actually, there are two ways. You can create the primary key either in your CREATE TABLE command or<br />

with an ALTER TABLE command.<br />

Much of the rest of this chapter makes use of the Accounting database you created in Chapter 5. The<br />

assumption is that the Accounting database is as it was at the end of Chapter 5 after using the Management<br />

Studio to create tables in the database.<br />

Creating the Primary Key at Table Creation<br />

158<br />

Don’t confuse the primary key, which uniquely identifies each row in a table, with a<br />

GUID, which is a more generic tool, typically used to identify something (it could be<br />

anything, really) across all space and time. While a GUID can certainly be used as a<br />

primary key, they incur some overhead, and are usually not called for when we’re<br />

only dealing with the contents of a table. Indeed, the only common place that a<br />

GUID becomes particularly useful in a database environment is as a primary key<br />

when dealing with replicated or other distributed data.<br />

Let’s review one of our CREATE TABLE statements from the previous chapter:<br />

CREATE TABLE Customers<br />

(<br />

CustomerNo int IDENTITY NOT NULL,<br />

CustomerName varchar(30) NOT NULL,<br />

Address1 varchar(30) NOT NULL,<br />

Address2 varchar(30) NOT NULL,<br />

City varchar(20) NOT NULL,<br />

State char(2) NOT NULL,<br />

Zip varchar(10) NOT NULL,<br />

Contact varchar(25) NOT NULL,<br />

Phone char(15) NOT NULL,<br />

FedIDNo varchar(9) NOT NULL,<br />

DateInSystem smalldatetime NOT NULL<br />

);<br />

This CREATE statement should seem old hat by now, but it’s missing a very important piece — our<br />

PRIMARY KEY constraint. We want to identify CustomerNo as our primary key. Why CustomerNo? Well,<br />

we’ll look into what makes a good primary key in the next chapter, but for now, just think about it a bit.

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

Saved successfully!

Ooh no, something went wrong!