Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 6: Constraints A table can have a maximum of one primary key. As I mentioned earlier, it is rare to have a table in which you don’t want a primary key. When I say “rare” here, I mean very rare. A table that doesn’t have a primary key severely violates the concept of relational data — it means that you can’t guarantee that you can relate to a specific record. The data in your table no longer has anything that gives it distinction. Situations where you can have multiple rows that are logically identical are actually not that uncommon, but that doesn’t mean that you don’t want a primary key. In these instances, you’ll want to take a look at fabricating some sort of key. This approach has most often been implemented using an identity column, although using a GUID is now an appropriate alternative in some situations. A primary key ensures uniqueness within the columns declared as being part of that primary key, and that unique value serves as an identifier for each row in that table. How do we create a primary key? Actually, there are two ways. You can create the primary key either in your CREATE TABLE command or with an ALTER TABLE command. Much of the rest of this chapter makes use of the Accounting database you created in Chapter 5. The assumption is that the Accounting database is as it was at the end of Chapter 5 after using the Management Studio to create tables in the database. Creating the Primary Key at Table Creation 158 Don’t confuse the primary key, which uniquely identifies each row in a table, with a GUID, which is a more generic tool, typically used to identify something (it could be anything, really) across all space and time. While a GUID can certainly be used as a primary key, they incur some overhead, and are usually not called for when we’re only dealing with the contents of a table. Indeed, the only common place that a GUID becomes particularly useful in a database environment is as a primary key when dealing with replicated or other distributed data. Let’s review one of our CREATE TABLE statements from the previous chapter: CREATE TABLE Customers ( CustomerNo int IDENTITY NOT NULL, CustomerName varchar(30) NOT NULL, Address1 varchar(30) NOT NULL, Address2 varchar(30) NOT NULL, City varchar(20) NOT NULL, State char(2) NOT NULL, Zip varchar(10) NOT NULL, Contact varchar(25) NOT NULL, Phone char(15) NOT NULL, FedIDNo varchar(9) NOT NULL, DateInSystem smalldatetime NOT NULL ); This CREATE statement should seem old hat by now, but it’s missing a very important piece — our PRIMARY KEY constraint. We want to identify CustomerNo as our primary key. Why CustomerNo? Well, we’ll look into what makes a good primary key in the next chapter, but for now, just think about it a bit.

Do we want two customers to have the same CustomerNo? Definitely not. It makes perfect sense for a CustomerNo to be used as an identifier for a customer. Indeed, such a system has been used for years, so there’s really no sense in reinventing the wheel here. To alter our CREATE TABLE statement to include a PRIMARY KEY constraint, we just add in the constraint information right after the column(s) that we want to be part of our primary key. In this case, we would use: USE Accounting; CREATE TABLE Customers ( CustomerNo int IDENTITY NOT NULL PRIMARY KEY, CustomerName varchar(30) NOT NULL, Address1 varchar(30) NOT NULL, Address2 varchar(30) NOT NULL, City varchar(20) NOT NULL, State char(2) NOT NULL, Zip varchar(10) NOT NULL, Contact varchar(25) NOT NULL, Phone char(15) NOT NULL, FedIDNo varchar(9) NOT NULL, DateInSystem smalldatetime NOT NULL ); Since this table already exists in our Accounting database (as we left it at the end of Chapter 5), we need to also drop the table prior to running the CREATE TABLE script (i.e., DROP TABLE Customers). Notice that we altered one line (all we did was remove the comma) and added some code on a second line for that column. It was easy! Again, we just added one simple keyword (OK, so it’s two words, but they operate as one) and we now have ourselves a primary key. Creating a Primary Key on an Existing Table Now, what if we already have a table and we want to set the primary key? That’s also easy. We’ll do that for our Employees table: USE Accounting ALTER TABLE Employees ADD CONSTRAINT PK_EmployeeID PRIMARY KEY (EmployeeID); Our ALTER command tells SQL Server: ❑ That we are adding something to the table (we could also be dropping something from the table if we so chose) ❑ What it is that we’re adding (a constraint) ❑ What we want to name the constraint (to allow us to address the constraint directly later) ❑ The type of constraint (PRIMARY KEY) ❑ The column(s) that the constraint applies to Chapter 6: Constraints 159

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!