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.

6<br />

Constraints<br />

You’ve heard me talk about them, but now it’s time to look at them seriously — it’s time to deal<br />

with constraints. We’ve talked a couple of times already about what constraints are, but let’s review<br />

in case you decided to skip straight to this chapter.<br />

A constraint is a restriction. Placed at either column or table level, a constraint<br />

ensures that your data meets certain data integrity rules.<br />

This goes back to the notion that I talked about back in Chapters 1 and 2, that ensuring data integrity<br />

is not the responsibility of the programs that use your database, but rather the responsibility of the<br />

database itself. If you think about it, this is really cool. Data is inserted, updated, and deleted from<br />

the database by many sources. Even in stand-alone applications (situations where only one program<br />

accesses the database), the same table may be accessed from many different places in the program. It<br />

doesn’t stop there though. Your database administrator (that might mean you if you’re a dual-role<br />

kind of person) may be altering data occasionally to deal with problems that arise. In more complex<br />

scenarios, you can actually run into situations where literally hundreds of different access paths<br />

exist for altering just one piece of data, let alone your entire database.<br />

Moving the responsibility for data integrity into the database itself has been revolutionary to database<br />

management. There are still many different things that can go wrong when you are attempting<br />

to insert data into your database, but your database is now proactive rather than reactive to problems.<br />

Many problems with what programs allow into the database are now caught much earlier in the<br />

development process because, although the client program allowed the data through, the database<br />

knows to reject it. How does it do it? Primarily with constraints (data types and triggers are among<br />

the other worker bees of data integrity).<br />

In this chapter, we’ll be looking at the three different types of constraints at a high level:<br />

❑ Entity constraints<br />

❑ Domain constraints<br />

❑ Referential integrity constraints

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

Saved successfully!

Ooh no, something went wrong!