Get the latest news, exclusives, sport, celebrities, showbiz, politics, business and lifestyle from The VeryTime,Stay informed and read the latest news today from The VeryTime, the definitive source.

CHECK Constraints in Microsoft SQL Server 2008

29


CHECK constraints allow you to limit the types of data that users may insert in a database. They go beyond data types and allow you to define the specific values that may be included in a column.

For example, imagine that you have an inventory database that contains information about products available in a retail store. You might have a products table that contains item descriptions, product codes, and pricing information.

Generally speaking, you would use the smallmoney data type for your pricing information. However, that data type allows values in the range -$214,748.3648 to $214,748.3647. If your store has a policy limiting prices to a range of $0.00 to $50.00, you can implement that business requirement using a CHECK constraint.

Creating a CHECK Constraint

Before you can implement a CHECK constraint, you must be able to express it in a SQL statement. You can do this using any standard Transact-SQL conditions, including pattern matching wildcards. For example, you may write the pricing CHECK constraint described above as:
cost >= 0 and cost <=50

Applying a CHECK Constraint in SQL Server

Once you've written the Transact-SQL statement that describes your CHECK constraint, you may implement it in SQL Server 2008 as follows:
  1. Open SQL Server Management Studio
  2. Navigate to the server containing the database you wish to modify and click the "+" icon to its left
  3. Expand the Databases folder by clicking the "+" icon to its left


  1. Expand the folder for the database containing the table where you wish to implement the constraint
  2. Expand the Tables folder
  3. Right-click on the table where you would like to implement the constraint and select Design from the menu
  4. Click the Table Designer menu at the top of the screen
  5. Choose Check Constraints from the drop-down list
  6. Click Add to build a new CHECK constraint
  7. Type the constraint definition you created (for example, "cost >= 0 and cost <=50") in the Expression textbox.
  8. Click the Close button
  9. Choose File -> Save to save the constraint to the database
That's all there is to building constraints in Microsoft SQL Server 2008! You can now use this powerful tool to help you enforce business requirements in your databases.
Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.