Solved

Enforce Referential Integrity with mismatched primary keys

Posted on 2010-11-23
6
295 Views
Last Modified: 2012-05-10
Hi,

Scenario 1:
On table 1 you have 3 primary keys, on table 2 you reference table 1's primary keys through a foreign key and thereby enforcing referential integrity. All Good.

Scenario 2 which I'm trying to get right:
On table 1 you have 3 primary keys but on table 2 you only have 2 fieldS that match table 1's primary keys.  Is there an elegant way to enforce referential integrity?

I get that you can do it through a trigger, but it's too easy to disable a trigger so I'm aiming for something more hardcore like a foreign key.

I'm trying to get something like this to work:
ALTER TABLE [dbo].[TABLE2]  WITH CHECK ADD  CONSTRAINT [CYFOK3_TB2] FOREIGN KEY([TB2_PAGE], [TB2_TAB])
REFERENCES [dbo].[TABLE1] ([TB1_PAGE], [TB1_TAB])

ALTER TABLE [dbo].[TABLE2] CHECK CONSTRAINT [CYFOK3_TB2]

Thanks      
CREATE TABLE [dbo].[TABLE1](
	[TB1_PAGE] [nvarchar](6) NOT NULL,
	[TB1_TAB] [nvarchar](15) NOT NULL,
	[TB1_STATE] [nvarchar](12) NOT NULL DEFAULT ('*'),
	[TB1_SQLIDENTITY] [int] IDENTITY(1,1) NOT NULL,
	CONSTRAINT [CYPRIK_TB1] PRIMARY KEY NONCLUSTERED 
	(
		[TB1_PAGE] ASC,
		[TB1_TAB] ASC,
		[TB1_STATE] ASC
	)
	WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) 
	ON [PRIMARY],
	CONSTRAINT [CYUNQSQL_TB1] UNIQUE NONCLUSTERED 
	(
		[TB1_SQLIDENTITY] ASC
	)
	WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) 
	ON [PRIMARY]
) 
ON [PRIMARY]
GO


CREATE TABLE [dbo].[TABLE2](
	[TB2_PAGE] [nvarchar](6) NOT NULL,
	[TB2_TAB] [nvarchar](15) NOT NULL,
	[TB2_SQLIDENTITY] [int] IDENTITY(1,1) NOT NULL,
	CONSTRAINT [CYPRIK_TB2] PRIMARY KEY NONCLUSTERED 
	(
		[TB2_PAGE] ASC,
		[TB2_TAB] ASC
	)
	WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) 
	ON [PRIMARY],
	CONSTRAINT [CYUNQSQL_TB2] UNIQUE NONCLUSTERED 
	(
		[TB2_SQLIDENTITY] ASC
	)
	WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) 
	ON [PRIMARY]
) 
ON [PRIMARY]
GO

Open in new window

0
Comment
Question by:Cyber-Storm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 300 total points
ID: 34196373
if you have this kind of "design", the design is wrong aka not normlized.
you are likely missing some intermediate table, in your case, a table with indeed only the 2 columns as primary key, and table1 having the 2 fields (of the 3 being primary key) to be a foreign key to that table ...
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34196378
and to answer the question: a foreign key points to a primary key. you cannot have a foreign key point to a partial part of the primary key of the other table ...
0
 
LVL 6

Accepted Solution

by:
subhashpunia earned 200 total points
ID: 34196423
You can add another foreign key on Table1 on two columns refering to the two columns of Table1 like:

ALTER TABLE [dbo].[TABLE1]  WITH CHECK ADD  CONSTRAINT [CYFOK3_TB1] UNIQUE ([TB2_PAGE], [TB2_TAB])
GO
ALTER TABLE [dbo].[TABLE2]  WITH CHECK ADD  CONSTRAINT [CYFOK3_TB2] FOREIGN KEY([TB2_PAGE], [TB2_TAB])
REFERENCES [dbo].[TABLE1] ([TB1_PAGE], [TB1_TAB])
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:Cyber-Storm
ID: 34196539
@angel, none of that is helping any :(
I'm trying to establish referential integrity hence no you didn't answer the question. I know what a foreign key and primary key is, I was trying to offer an example.  Maybe I shouldn't be so "complete" in my explanations since the more I offer, the more you "geniuses" take things out of context

@subhash
Thanks, I thought of that too, but I would that not then prevent me from adding a state?  See it must be able to handle a page, tab and state on table 1, but table 2 only needs to refer to a page and a tab since it doesn't care about the state.

---

Table 1:
Page1, Tab1, State1
Page1, Tab1, State2
Page1, Tab2, State1
Page1, Tab2, State2
Page2, Tab1, State1
Page2, Tab2, State1
etc

Table 2:
Page1, Tab1 = Table1.Page1, Table1.Tab1, Table1.State1 and Table1.Page1, Table1.Tab1, Table1.State2
etc

I can add a trigger that will check that page and tab exists in table 1 before allowing an insert, update or delete.  But I was hoping for a more elegant solution to enforcing referential integrity like a foreign key constraint does natively.

Hope this adds a little more useful information for you guys.

Thanks,
Storm
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 300 total points
ID: 34196587
I can only repeat: unless you have a primary key on PageX and TabX somewhere, you cannot have a foreign key pointing to it. full stop.
That's the way PK <=> FK works, and as you say, you are already aware of that.

this does not result as a limitation of the syntax, but is a result of the "requirement" of where it comes from: which is normalization rules.

you can use a trigger to validate the existence, indeed.

note:
>On table 1 you have 3 primary keys
is actually wrong wording (might be language barrier ...)
the correct wording would be:
on table 1 there is 1 primary key over 3 columns

CHeers
0
 

Author Closing Comment

by:Cyber-Storm
ID: 34196676
Based on the 3 selected answers I was able to figure out my problem and implement an elegant solution.

Thanks guys :)
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS SQL + Insert Into Table - If Doesnt Exist 9 58
date diff with Fiscal Calendar 4 75
convert null in sql server 12 57
Getting local user timezone in Sql Server 5 30
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question