1

Quick question on SQL how can I make a check such as:

Alter Table Invoices WITH NOCHECK
ADD CHECK 

Now this is the part where I want to add a check so that the column PaymentDate can be null if another column Payment Total is equal to 0 and also that PaymentDate is not null if Payment Total is greater than 0.

2
  • 1
    MySQL or SQL Server? MySQL (still, afaik) does not honour check constraints at all (although it happily parses them) Commented Aug 11, 2013 at 18:22
  • What's wrong the docs? What is your specific question? Look into conditionals, look into null checking, and alter table syntax. This is assuming you mean MySQL and not SQL Server. Read the tag popouts before adding them. Commented Aug 11, 2013 at 18:26

1 Answer 1

5

Here is a way to do it :

Alter Table Invoices WITH NOCHECK
ADD CHECK (   (PaymentTotal > 0 AND PaymentDate IS NOT NULL) 
           OR (PaymentTotal = 0 ) ) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, as usual I was trying to way over complicate this when it did not need to be...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.