Solved

Enforce Referential Integrity with mismatched primary keys

Posted on 2010-11-23
6
290 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
Comment Utility
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]
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:Cyber-Storm
Comment Utility
@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
Comment Utility
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
Comment Utility
Based on the 3 selected answers I was able to figure out my problem and implement an elegant solution.

Thanks guys :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

10 Experts available now in Live!

Get 1:1 Help Now