CHECK Constraints

-- Table-level CHECK Constraints based on a function
-- Check Function1: Check OrderID

CREATE FUNCTION getOrderID(@id int)
RETURNS int
AS
BEGIN
        RETURN ISNULL((SELECT OrderID FROM OrderHeader WHERE OrderID = @id),0)
END

-- Use Check Function1
ALTER TABLE OrderDetail WITH NOCHECK ADD CONSTRAINT chk_order
        CHECK (dbo.getOrderID(OrderID) != 0)
   
UPDATE OrderDetail SET OrderID = 250 where OrderDetailID = 12

ALTER TABLE OrderDetail DROP CONSTRAINT chk_order

DROP FUNCTION getOrderID

你可能感兴趣的:(CHECK Constraints)