|
The following
Microsoft SQL Server T-SQL code sample illustrates how to setup a table level constraint for a
restaurant database:
-- Create constraint
ALTER TABLE dbo.RestaurantInventory
WITH NOCHECK ADD CONSTRAINT CK_RestaurantInventory_Type_Cost
CHECK ( (TYPE = 'Food'
AND COST < (100)
OR TYPE = 'Furniture'
AND COST > (10)
AND COST < (1000)) )
GO
-- Enable constraint
ALTER TABLE dbo.RestaurantInventory
CHECK CONSTRAINT CK_RestaurantInventory_Type_Cost
GO
This constraint
checks the range of cost for food items and restaurant furniture.
Related article:
Creating and Modifying CHECK Constraints
|