Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
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
- Page 146 and 147: Chapter 4: JOINs Figure 4-1 As alwa
- Page 148 and 149: Chapter 4: JOINs 110 CREATE TABLE U
- Page 150 and 151: Chapter 4: JOINs There are two diff
- Page 152 and 153: Chapter 5: Creating and Altering Ta
- Page 154 and 155: Chapter 5: Creating and Altering Ta
- Page 156 and 157: Chapter 5: Creating and Altering Ta
- Page 158 and 159: Chapter 5: Creating and Altering Ta
- Page 160 and 161: Chapter 5: Creating and Altering Ta
- Page 162 and 163: Chapter 5: Creating and Altering Ta
- Page 164 and 165: Chapter 5: Creating and Altering Ta
- Page 166 and 167: Chapter 5: Creating and Altering Ta
- Page 168 and 169: Chapter 5: Creating and Altering Ta
- Page 170 and 171: Chapter 5: Creating and Altering Ta
- Page 172 and 173: Chapter 5: Creating and Altering Ta
- Page 174 and 175: Chapter 5: Creating and Altering Ta
- Page 176 and 177: Chapter 5: Creating and Altering Ta
- Page 178 and 179: Chapter 5: Creating and Altering Ta
- Page 180 and 181: Chapter 5: Creating and Altering Ta
- Page 182 and 183: Chapter 5: Creating and Altering Ta
- Page 184 and 185: Chapter 5: Creating and Altering Ta
- Page 186 and 187: Chapter 5: Creating and Altering Ta
- Page 188 and 189: Chapter 5: Creating and Altering Ta
- Page 190 and 191: Chapter 5: Creating and Altering Ta
- Page 192 and 193: Chapter 6: Constraints At a more sp
- Page 194 and 195: Chapter 6: Constraints We can then
- Page 198 and 199: Chapter 6: Constraints FOREIGN KEY
- Page 200 and 201: Chapter 6: Constraints How It Works
- Page 202 and 203: Chapter 6: Constraints Cascading Ac
- Page 204 and 205: Chapter 6: Constraints 166 This is
- Page 206 and 207: Chapter 6: Constraints 168 FROM Ord
- Page 208 and 209: Chapter 6: Constraints 170 Let’s
- Page 210 and 211: Chapter 6: Constraints CHECK Constr
- Page 212 and 213: Chapter 6: Constraints Defining a D
- Page 214 and 215: Chapter 6: Constraints Ignoring Bad
- Page 216 and 217: Chapter 6: Constraints Try running
- Page 218 and 219: Chapter 6: Constraints Rules and De
- Page 220 and 221: Chapter 6: Constraints Dropping Rul
- Page 222 and 223: Chapter 6: Constraints 184 Restrict
- Page 225 and 226: 7 Adding More to Our Queries When I
- Page 227 and 228: Building a Nested Subquery A nested
- Page 229 and 230: While this works just fine, queries
- Page 231 and 232: We’ll go back to the AdventureWor
- Page 233 and 234: ❑ Aliases are used in both querie
- Page 235 and 236: Now let’s see this at work in our
- Page 237 and 238: So let’s take this now and apply
- Page 239 and 240: This join-based syntax, for example
- Page 241 and 242: IF NOT EXISTS (SELECT ‘True’ FR
- Page 243 and 244: The conversions can actually get a
- Page 245 and 246: there are no other roll up records
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.