Solved

Enforce Referential Integrity with mismatched primary keys

Posted on 2010-11-23
6
293 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
  • 3
  • 2
6 Comments
 
LVL 142

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 142

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 142

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

832 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