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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:23:54+00:00 2026-06-04T18:23:54+00:00

I have 3 tables in SQL Server 2008 R2 that look like these: A

  • 0

I have 3 tables in SQL Server 2008 R2 that look like these:

DB Model

A COMPANY may have many LSPs. An LSP may have many SERVICEs.

And I need to make sure that SERVICE_CODE uniquely identifies a SERVICE record within a COMPANY. In other words, COMPANY_ID + SERVICE_CODE should uniquely identify a SERVICE record in the entire system.

For example: COMPANY-A may NOT have 2 services (with 2 different SERVICE_IDs) with the same SERVICE_CODE. But COMPANY-A and COMPANY-B may both have 2 separate SERVICES (again, with different SERVICE_IDs) with SERVICE_CODE = “PREMIUM”.

I need something like this:

alter table "SERVICE" 
add constraint "SERVICE_Index01" 
unique ("COMPANY_ID", "SERVICE_CODE") 

But (obviously) this fails because the COMPANY_ID column is not in the SERVICE table.

Thanks in advance for any help.

  • 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-06-04T18:23:56+00:00Added an answer on June 4, 2026 at 6:23 pm

    You could use an indexed view as an external constraint:

    CREATE VIEW dbo.CompanyServices
    WITH SCHEMABINDING
    AS
    SELECT
      c.COMPANY_ID,
      s.SERVICE_CODE
    FROM       dbo.COMPANY c
    INNER JOIN dbo.LSP     l ON c.COMPANY_ID = l.COMPANY_ID
    INNER JOIN dbo.SERVICE s ON l.LSP_ID     = s.LSP_ID
    GO
    
    CREATE UNIQUE CLUSTERED INDEX UQ_CompanyServices
    ON dbo.CompanyServices (COMPANY_ID, SERVICE_CODE);
    

    The index will make sure there’s no duplicates of (COMPANY_ID, SERVICE_CODE) in your data.

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

Sidebar

Related Questions

I have a Microsoft SQL Server 2008 query that returns data from three tables
i have four tables like below in sql server 2008 : TABLE 1 ->
I have data in a SQL Server 2008 table that looks like this: I
I have created a MedicalLabResult table on Sql server 2008 that have a Result
I'm using SQL Server 2008 and I have a table that has a 'Status'
I have a table which contains my ads that can be searched in sql-server-2008.
I have 2 Identical tables in SQL Server 2008, one is the Live table
In SQL Server 2008: I have two tables, dtlScheme and dtlRenewal, with a one
In SQL Server 2008 I have two tables. One table for users: id_user -
SQL Server 2008 Database Question. I have 2 tables, for arguments sake called Customers

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.