Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex Check Constraint?

I have a customers table that links to an addresses table through a middle CustomerAddress table. This means that a customer can have many addresses and an address can have many customers. (This is neccessary due to the fact that we deal with spouses and children as seperate customers, and each can have delivery, work, billing and other addresses).

I want a customer to be able to specify a preferred address.

My thought was to create a new column in the customers table that links to a CustomerAddress record.

My problem is- how can I ensure that the selected preferred address is one of that customers addresses?

My thought was to put a Check constraint on the customers.preferredAddress field that checks the given CustomerAddress to see if that records customer ID matches the customer being updated.

Is this possible? I have only ever used Check constraints to check simple stuff like (Value > 0) etc.

Thanks for your help

like image 225
Steven Wood Avatar asked Feb 24 '14 21:02

Steven Wood


People also ask

What is CK constraint?

The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

What are the limitations of check constraint?

The condition must be a Boolean expression evaluated using the values in the row being inserted or updated and can't contain sub queries, sequence, the SYSDATE,UID,USER or USERENV SQL functions, or the pseudo columns LEVEL or ROWNUM.

What does CHECK constraints mean?

A check constraint is a rule that specifies the values that are allowed in one or more columns of every row of a base table. For example, you can define a check constraint to ensure that all values in a column that contains ages are positive numbers.


1 Answers

Write a UDF for verifying address ownership, then reference that UDF from a check constraint.

CREATE FUNCTION dbo.fnIsAddressOwner (
  @CustomerId int,
  @AddressId int
)
RETURNS tinyint
AS
BEGIN
  DECLARE @Result tinyint
  IF EXISTS(SELECT * FROM CustomerAddresses WHERE CustomerId=@CustomerId and AddressId=@AddressId)
    SET @Result= 1
  ELSE 
    SET @Result= 0
  RETURN @Result
END


CREATE TABLE Customers (
  CustomerId int,
  PreferredAddressId int,
  CONSTRAINT ckPreferredAddressId CHECK (
    dbo.fnIsAddressOwner(CustomerId, PreferredAddressId) = 1)
  )
)
like image 110
Keith Avatar answered Sep 30 '22 16:09

Keith