Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7654605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:17:54+00:00 2026-05-31T12:17:54+00:00

I have a fairly simple check constraint, where I want to force two columns

  • 0

I have a fairly simple check constraint, where I want to force two columns to be either both NULL or both NOT NULL. However INFORMATION_SCHEMA.CHECK_CONSTRAINTS does not properly show this constraint.

I basically want this constraint:

 ((Col1 IS NULL AND Col2 IS NULL) OR (Col1 IS NOT NULL AND Col2 IS NOT NULL))

but INFORMATION_SCHEMA.CHECK_CONSTRAINTS.CHECK_CLAUSE shows this:

 ([Col1] IS NULL AND [Col2] IS NULL OR [Col1] IS NOT NULL AND [Col2] IS NOT NULL)
--^                                ^  ^                                        ^
--missing parenthesis 

which is not the same and which is incorrect.

You can easily reproduce this…

create table and check constraint:

CREATE TABLE dbo.MyTest (RowID int identity(1,1) primary key, Col1 int NULL, Col2 int NULL)
ALTER TABLE dbo.MyTest ADD CONSTRAINT CK_MyTest_Cols CHECK ((Col1 IS NULL AND Col2 IS NULL) OR (Col1 IS NOT NULL AND Col2 IS NOT NULL))

display incorrect INFORMATION_SCHEMA.CHECK_CONSTRAINTS.CHECK_CLAUSE:

SELECT
     c.ORDINAL_POSITION
        ,cc.CONSTRAINT_NAME
        ,cc.CHECK_CLAUSE
    FROM INFORMATION_SCHEMA.COLUMNS                                  c
        LEFT OUTER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE   cu ON c.TABLE_CATALOG=cu.TABLE_CATALOG AND c.TABLE_SCHEMA=cu.TABLE_SCHEMA AND c.TABLE_NAME=cu.TABLE_NAME AND c.COLUMN_NAME=cu.COLUMN_NAME
        LEFT OUTER JOIN INFORMATION_SCHEMA.CHECK_CONSTRAINTS         cc ON cu.TABLE_CATALOG=cc.CONSTRAINT_CATALOG AND cu.TABLE_SCHEMA=cc.CONSTRAINT_SCHEMA AND cu.CONSTRAINT_NAME=cc.CONSTRAINT_NAME
    WHERE c.TABLE_SCHEMA='dbo' AND c.TABLE_Name='MyTest' AND cc.CONSTRAINT_SCHEMA IS NOT NULL

verify that the check constraint is actually working as desired:

INSERT INTO dbo.MyTest (Col1, Col2) VALUES (NULL,NULL)
INSERT INTO dbo.MyTest (Col1, Col2) VALUES (5,5)
INSERT INTO dbo.MyTest (Col1, Col2) VALUES (5,NULL)

what is up with INFORMATION_SCHEMA.CHECK_CONSTRAINTS? and is there anything I’m doing wrong or any workaround for this?

SSMS shows it incorrectly as well when using the check constraints duologue box (when Designing the table). It will also script it incorrectly as well. However, when I use the SSMS script of this constraint, the three inserts in my test (above) still work as desired (first two inserts work, third fails)? This seems contrary to everything I learned about AND, OR and parenthesis. What is going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:17:56+00:00Added an answer on May 31, 2026 at 12:17 pm

    They are the same. See Logical Operator Precedence

    When more than one logical operator is used in a statement, NOT is
    evaluated first, then AND, and finally OR.

    And has higher precedence than OR so the brackets are optional – In the same way as multiplication has higher precedence than addition so

    SELECT 1 * 1 + 3 * 3 
    

    is the same as

    SELECT (1 * 1) + (3 * 3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple class that I want to save to SQL Server
I have fairly simple problem but I can not think of the simple solution.
I have a Core Data app with a fairly simple data model. I want
I have a fairly simple project with only two XIBs, 5 custom classes and
I'm having trouble crafting a fairly simple query with Doctrine... I have two arrays
I have a fairly simple block of code. Sub Run(Name) on error resume next
I have a fairly simple python loop that calls a few functions, and writes
I have a fairly simple cell in a table with an inline style: <td
I have a fairly simple set of functionality for which I have multiple implementations,
I have a fairly simple Linq query (simplified code): dim x = From Product

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.