?
Solved

Enforce Referential Integrity with mismatched primary keys

Posted on 2010-11-23
6
Medium Priority
?
299 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 1200 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 800 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 1200 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

777 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