Hitchhikers Guide to Visual Studio and SQL Server: Best Practice Architectures and Examples, 7th Edition (Microsoft Windows Server System Series)
| SQL Server implements a whole family of constraints. These are used for a variety of purposes but generally are there to make sure your database integrity is maintainedboth data integrity and referential integrity. Yes, constraints can be used to enforce business rules, and I alluded to that earlier in this section. SQL Server supports the following types of constraints:
This constraint has several options of its own to dictate what should happen when T-SQL tries to delete or update a row that's tied to another table with a PK/FK relationship. These options are:
Each of these constraints can be established using the SQL Server Management Studio IDE. While the dialog to set constraints is carefully hidden, if you open a connection to the target server and open the Object Explorer, drill into the list of Tables, choose a specific table, right-click and choose "Modify", you'll see a two-part dialog that can be used to modify the table. To access the table's Properties, right-click on the white-space next to the column definitions (at the top). This opens a dialog that supports setting the constraints, as shown in Figure 2.64. Figure 2.64. Opening the constraints selector dialog.
This dialog is your gateway to the UI dialogs used to add and delete table columns; set or remove the primary key (and set the PRIMARY KEY constraint); maintain PK/FK relationships; maintain table, FullText, and XML indexes; as well as maintain Check Constraints. For example, to create the CHECK constraint defined earlier on the Discount column, you could click on "Check Constraints..." and fill in the dialog as shown in Figure 2.65. Notice that the syntax for the constraint expression is a bit different. In this case, simply refer to the column to be tested by name. In the constraint I created, I'm still validating the value to be between 0 and 99. Figure 2.65. Creating a CHECK constraint with the SQL Server Management Studio.
|
Категории