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

  • Home
  • SEARCH
  • 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 6086961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:48:56+00:00 2026-05-23T11:48:56+00:00

I have the following T-SQL schema. The problem I am having is that the

  • 0

I have the following T-SQL schema. The problem I am having is that the check constraint on the Download table is not working. I am still able to insert records into that table that contain NULL values for both ProductId and CategoryId. Why is this so?

I want both the ProductId and CategoryId columns to allow NULL values, but for any given record only one of these are allowed to be set to NULL, the other needs to be a corresponding Id of the Category or Product table.

CREATE TABLE Category (
    Id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
    Description nvarchar(100) NULL,
    ParentCategoryId int NULL
    CONSTRAINT fk_CategoryId_CategoryId FOREIGN KEY (Id) REFERENCES Category(Id)
)
GO

CREATE TABLE Product (
    Id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
    Title nvarchar(100) NOT NULL,
    TagLine nvarchar(MAX) NOT NULL,
    Description nvarchar(MAX)NULL,
    CategoryId int NOT NULL,
    ImageUrl nvarchar(255) NULL,
    Keywords nvarchar(200) NOT NULL

    CONSTRAINT fk_ProductCategoryId_CategoryId FOREIGN KEY (CategoryId) REFERENCES Category(Id)
)
GO

CREATE TABLE Download (
    Id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
    Title nvarchar(100) NOT NULL,
    Description nvarchar(MAX) NULL,
    CategoryId int NULL,
    ProductId int NULL,
    DownloadUrl nvarchar(255) NOT NULL,

    CONSTRAINT fk_DownloadCategoryId FOREIGN KEY (CategoryId) REFERENCES Category(Id),
    CONSTRAINT fk_DownloadProductId FOREIGN KEY (ProductId) REFERENCES Product(Id),
    CONSTRAINT chk_ReferencesCategoryOrProduct CHECK (ProductID != NULL AND CategoryId != NULL)
)
GO
  • 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-23T11:48:57+00:00Added an answer on May 23, 2026 at 11:48 am

    Use:

    CONSTRAINT chk_ReferencesCategoryOrProduct CHECK (ProductID IS NOT NULL 
                                                   OR CategoryId IS NOT NULL)
    

    NULL isn’t a value — it’s a placeholder for the lack of a value. Which is why you need to use specific syntax to check for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I have a DB table that uses the following schema: CREATE TABLE users (id
I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
I have the following Sql Query that returns the type of results that I
I have the following SQL table - Date StoreNo Sales 23/4 34 4323.00 23/4
I have the following schema: TABLE bands -> band_id -> property1 -> property2 ->
I have the following database schema (Microsoft SQL Server Compact Edition): How can I
I have a pretty simple table in SQLite, with the following schema: CREATE TABLE
I have the following sql query for transforming data but is it possible to
I have the following SQL query: select expr1, operator, expr2, count(*) as c from

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.