Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to Enforce Foreign Key  in composite Primary Key?

Posted on 2012-09-19
2
Medium Priority
?
343 Views
Last Modified: 2012-10-03
How to Enforce Froeign Key  for composite Primaey Key without introducing any surrogate  keys?
0
Comment
Question by:venk_r
2 Comments
 
LVL 5

Accepted Solution

by:
25112 earned 2000 total points
ID: 38414663
Primary keys are unique by definition. So you should be OK without surrogate key issues..

look below:


create table A
(one int not null,
two int not null,
three int not null,
four datetime)

create table b
(one int not null,
two int not null,
three int not null ,
four datetime)


ALTER TABLE A ADD  CONSTRAINT [PK_TEST] PRIMARY KEY CLUSTERED
(
      one ASC,two ASC,three ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO


ALTER TABLE B  WITH CHECK ADD  CONSTRAINT [FK_TEST] FOREIGN KEY(one,two,three)
REFERENCES A (one,two,three)
GO

DROP TABLE B
DROP TABLE A
0
 
LVL 8

Author Closing Comment

by:venk_r
ID: 38460800
thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

864 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