Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a unique constraint using a column from another table?

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.

like image 493
Cesar Daniel Avatar asked May 31 '12 03:05

Cesar Daniel


People also ask

How do you add a unique constraint to a column?

To create a unique constraintIn Object Explorer, right-click the table to which you want to add a unique constraint, and select Design. On the Table Designer menu, select Indexes/Keys. In the Indexes/Keys dialog box, select Add.

Can a foreign key reference a unique constraint?

A UNIQUE constraint can be referenced by a FOREIGN KEY constraint. When a UNIQUE constraint is added to an existing column or columns in the table, by default, the Database Engine examines the existing data in the columns to make sure all values are unique.

Can a same constraint be used in 2 tables?

Yes you can do this from this example i didn't test it, but it should work.


2 Answers

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.

like image 176
Andriy M Avatar answered Oct 03 '22 06:10

Andriy M


Is each company limited to a single LSP? Is Service_Code unique (or could there be two service codes "PREMIUM" with different Service_IDs)?

CREATE TABLE dbo.Company
(
  CompanyID INT PRIMARY KEY
  -- , ...
);

CREATE TABLE dbo.LSP
(
  LSPID      INT PRIMARY KEY, 
  CompanyID  INT FOREIGN KEY REFERENCES dbo.Company(CompanyID) -- UNIQUE?
  -- , ...
);

CREATE TABLE dbo.Service
(
  ServiceID    INT PRIMARY KEY
  -- , ...
);

CREATE TABLE dbo.LSP_Service
(
  LSPID        INT FOREIGN KEY REFERENCES dbo.LSP(LSPID), 
  ServiceID    INT FOREIGN KEY REFERENCES dbo.Service(ServiceID), 
  PRIMARY KEY (LSPID, ServiceID)
);
like image 27
Aaron Bertrand Avatar answered Oct 03 '22 06:10

Aaron Bertrand