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 3627488
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:53:42+00:00 2026-05-18T23:53:42+00:00

In MS SQL server, can i use a SELECT statement to define a CHECK

  • 0

In MS SQL server, can i use a SELECT statement to define a CHECK constraint? Say i have to work with two tables “Customer Master” and “Indian Customer” in ideal situation both tables are compleatly different, and are not interrelated in anyways. however they share the same database

Content of "Customer Master":
CustomerName (colomn): a, b, c, d, e
Branchlocation (colomn): IN, AU, IN, IN, UK

Content of "Indian Customer":
customerID (colomn): 1, 2, 3
CustomerName  (colomn): a, c, d
customer details (colomn): details1, details, details
.
.
.

In Table “Indian Customer” i want to put a constraint so that the users entring data in this table should not be able to enter customers that dont exist in “Customer Master” or whose branch location is not IN. also the tables are in the same project but are NOT directly related . In other words you can say Only indian customer from “Customer Master” should be in table “Indian Customer”.

select CustomerName from "Customer Master"
where Branchlocation = 'IN'

the output of the above query should only be allowed in [“Indian Customer”].[CustomerName]

  • 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-18T23:53:42+00:00Added an answer on May 18, 2026 at 11:53 pm

    You can add some additional constraints and superkeys, and get what you want:

    CREATE TABLE CustomerMaster (
         CustomerName varchar(100) not null,
         LocationCode char(2) not null,
         constraint PK_CustomerMaster PRIMARY KEY (CustomerName),
         constraint UQ_CustomerMaster_Location UNIQUE (CustomerName,LocationCode), /* <-- Superkey here */
         constraint CK_CustomerMaster_Locations CHECK (
             LocationCode in ('IN','UK','AU')
    )
    
    CREATE TABLE IndianCustomer (
         CustomerID int not null,
         CustomerName varchar(100) not null,
         CustomerDetails varchar(max) not null,
         LocationCode as 'IN' persisted,
         constraint FK_IndianCustomer_CustomerMaster FOREIGN KEY (CustomerName,LocationCode) references CustomerMaster (CustomerName,LocationCode)
    )
    

    By having LocationCode as a computed column in IndianCustomer, and having the foreign key against the superkey, you’re ensuring the data matches.

    You can define an additional FK constraint just for CustomerName -> CustomerName, this can prove useful in some circumstances.


    Or, to put it another way – there is one, highly stylised way to construct a constraint based on a “select” statement – and that is a FOREIGN KEY. But you sometimes have to add additional information (such as super keys, computed columns) to satisfy additional filtering requirements.

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

Sidebar

Related Questions

In SQL Server you can use the IsNull() function to check if a value
In Oracle / SQL Server I can use the WITH keyword to define a
I have a project that can use either SQL Server or MS Access as
In SQL Server I can use this: DECLARE @PageNum AS INT; DECLARE @PageSize AS
In SQL server you can use the DATENAME function to get the day of
In SQL Server you can use an XML datatype and map it to relational
i am using sql server 2000 can i use full text search on the
I can use the sql server management studio to open a sqlserver 2000 database,
I'm using SQL Server 2005 and I only can use Management Studio (it's a
In SQL Server 2017, you can use this syntax, but not in earlier versions:

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.