Chapter 6

Keys and Constraints


  • What kinds of constraints are available to enforce data integrity
  • Uses and syntax for entity constraints, including primary keys and unique constraints
  • Uses and syntax for domain constraints, including CHECK and DEFAULT constraints
  • How to create and use foreign key constraints as referential integrity constraints
  • How to temporarily disable constraints
  • Uses of rules and defaults
  • How to choose among all these options

You’ve heard me talk about them, but now it’s time to look at them seriously — it’s time to deal with constraints. I’ve talked a couple of times already about what constraints are, but here’s a review in case you decided to skip straight to this chapter.

imageNOTE A constraint is a restriction. Placed at either column or table level, a constraint ensures that your data meets certain data integrity rules.

This goes back to the notion that I talked about back in Chapters 1 and 2, that ensuring data integrity is not the responsibility of the programs that use your database, but rather the responsibility of the database itself. If you think about it, this is really cool. Data is inserted, updated, and deleted from the database by many sources. Even in standalone applications (situations where only one program accesses the database), the same table may be accessed from many different places in the program. ...

Get Beginning Microsoft® SQL Server® 2012 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.