Solved

Enforce Referential Integrity with mismatched primary keys

Posted on 2010-11-23
6
291 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Caste datetime 2 52
how to check the index used by a MS SQL queries or SP 7 39
Restrict result set 1 35
SQL server is using more virtual memory. 5 68
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now