1

I have four SQL Server tables. The first two, say tblProducts and tblSiteIds, each have a pointer to tblProviders

tblProducts: prod1 provider1
             prod2 provider1
             prod3 provider2
tblSiteIds:  siteId1 site1 idfromprovider1 provider1
             siteId2 site1 idfromprovider2 provider2
             siteId3 site2 idfromprovider2 provider2

[site1, site2, point to yet another table tblSites] [siteId? is the internal entry # for that row, idfromprovider is the actual free-text id] So tblSiteIds holds the id for a particular site, from a provider.

A third table, say tblOrders, has a pointer to each of those two:

tblOrder:    order1 siteId1 prod1
             order2 siteId1 prod2
             order3 siteId3 prod2

The issue I'm having is that there should be a rule that the siteId and the product for a given order must point to the same provider (broken in order3 here, since siteId3 comes from provider2, but prod2 comes from provider1), but I'm not sure how to enforce it. Should the tables be designed differently (how?), or it there a way to make this rule work? Thanks!

Here is how to reproduce this problem;

IF OBJECT_ID('tempdb..#tblProducts') IS NOT NULL DROP TABLE #tblProducts
CREATE TABLE #tblProducts (Prod int, Provider int)
INSERT INTO #tblProducts (Prod, Provider)
VALUES (1,1),(2,1),(3,2)

IF OBJECT_ID('tempdb..#tblSiteIds') IS NOT NULL DROP TABLE #tblSiteIds
CREATE TABLE #tblSiteIds (SiteID int, Site int, IDFromProvider int, Provider int)
INSERT INTO #tblSiteIds (SiteID, Site, IDFromprovider, Provider)
VALUES (1,1,1,1),(2,1,2,2),(3,2,2,2)

IF OBJECT_ID('tempdb..#tblOrders') IS NOT NULL DROP TABLE #tblOrders
CREATE TABLE #tblOrders (orderID int,siteId int,prod int)
INSERT INTO #tblOrders (orderID, siteId, prod)
VALUES (1,1,1),(2,1,2),(3,3,2)

/* this shows that for orderID 3 the providers are different */
SELECT
     o.orderID
    ,p.Provider
    ,s.Provider
FROM #tblOrders o
JOIN #tblProducts p ON o.prod = p.Prod
JOIN #tblSiteIds s ON o.siteId = s.SiteID
8
  • When you say "pointer" do you really mean a foreign key? It would help greatly if you could post the ddl for your tables and a few rows of sample data. As it sits right now I can't understand the issue.
    – Sean Lange
    Commented Aug 31, 2017 at 13:33
  • 1
    Your tblProducts and tblSiteIds tables look completely fine to me, each representing a one-to-many relationship. I am lost with the orders table. Maybe you could describe the logic behind what you think an order represents here? Commented Aug 31, 2017 at 13:33
  • Please use the following to post a question with enough information for us to respond: spaghettidba.com/2015/04/24/…
    – Eli
    Commented Aug 31, 2017 at 13:54
  • Without knowing why you made it this way, it seems very suspect to me that Provider is a property of both Product and Site. This USUALLY is a design flaw. Commented Aug 31, 2017 at 13:58
  • I hope you don't mind, I've added in the steps to reproduce this for testing purposes. Commented Aug 31, 2017 at 14:14

2 Answers 2

0

You can do it but note you'll have extra difficulties when updating referenced tables. Demo

CREATE TABLE tblProducts (
 ProdId int PRIMARY KEY,
 ProviderId int ,
 CONSTRAINT U_Product UNIQUE(ProdId, ProviderId)
 );
INSERT INTO tblProducts (ProdId, ProviderId)
VALUES (1,1),(2,1),(3,2);

CREATE TABLE tblSiteIds (
 SiteID int PRIMARY KEY,
 Site int, 
 IDFromProvider int, 
 ProviderId int,
 CONSTRAINT U_Site UNIQUE(SiteId, ProviderId))
INSERT INTO tblSiteIds (
 SiteID, 
 Site, 
 IDFromprovider, 
 ProviderId)
VALUES (1,1,1,1),(2,1,2,2),(3,2,2,2);

CREATE TABLE tblOrders (
 orderId int PRIMARY KEY,
 siteId int,
 prodId int,
 ProviderId int,
 CONSTRAINT FK1 FOREIGN KEY (ProdId, ProviderId) REFERENCES  tblProducts(ProdId, ProviderId),
 CONSTRAINT FK2 FOREIGN KEY (SiteId, ProviderId) REFERENCES  tblSiteIds(SiteId, ProviderId)
 );
-- OK
INSERT INTO tblOrders (orderId, siteId, prodId,ProviderId)
VALUES (1,1,1,1),(2,1,2,1);
-- fails
INSERT INTO tblOrders (orderId, siteId, prodId,ProviderId)
VALUES (3,3,2,2);

Now when updating tblSiteIds and setting another provider, update will fail till referencing orders are deleted or updated accordingly.

3
  • Could you explain this answer? How do you reference providerId in tblOrder, when it isn't directly a field there? Thanks; I think I'm missing the point here.
    – Mike Arrh
    Commented Aug 31, 2017 at 23:56
  • Yes, you need to add providerId into tblOrder.
    – Serg
    Commented Sep 1, 2017 at 7:00
  • See edited answer, using the code from your edited question
    – Serg
    Commented Sep 1, 2017 at 7:23
0

I was able to use the answer from Serg, but I'm wondering why this answer isn't more direct: Define functions

CREATE FUNCTION [dbo].[getProvIDfromProduct](@pdID int)
RETURNS int
AS
BEGIN
  DECLARE @r int
  select @r = providerId from tblProducts where prodId = @pdID
  RETURN @r
END

CREATE FUNCTION [dbo].[getProvIDfromProvNum](@provind int)
RETURNS int
AS
BEGIN
  DECLARE @r int
  select @r = providerId from tblSiteIds where siteId = @provind
  RETURN @r
END

then require them to match

ALTER TABLE tblOrders
  ADD CONSTRAINT ProdCheck
  CHECK (dbo.getProvIDfromProduct](prodId)=dbo.getProvIDfromProvNum(siteId))

Probably there's an easier way to do this than defining functions, but that's the idea: use a CHECK constraint to make the two columns' hidden fields match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.