Vlad11
Vlad11

Reputation: 103

Checking Constraints SQL

I have 2 columns in a table -

[SendDate] and [PayDate]

I am trying to implement a constraint that will check that - the product cannot be paid for if it has not been sent out.

I am not sure how to go on about it.

Any suggestions much appreciated, thanks

Upvotes: 3

Views: 57

Answers (1)

potashin
potashin

Reputation: 44581

You can try something like this:

CHECK( 
  (SendDate IS NULL AND PayDate IS NULL) OR 
  (SendDate IS NOT NULL AND PayDate IS NULL) OR 
  (SendDate IS NOT NULL AND PayDate > SendDate)
 );"

i.e. :

  1. order was not sent
  2. order was sent but was not paid
  3. order was sent and then paid

Upvotes: 2

Related Questions