Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Composite Primary Key

Posted on 2003-11-10
3
Medium Priority
?
1,581 Views
Last Modified: 2008-03-03
How can i implement :
Foreign key of one table referencing composite primary key of other table?
0
Comment
Question by:ewhopcis
[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
  • 2
3 Comments
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9717568
Try this

Alter table table2
add CONSTRAINT fkey FOREIGN KEY (id1, id2 ) REFERENCES dbo.table1


id1, id2 columns exists in both Table1, Table2
0
 

Author Comment

by:ewhopcis
ID: 9732319
can i define the composite foreign key while creating the table?
0
 
LVL 15

Accepted Solution

by:
namasi_navaretnam earned 120 total points
ID: 9732433
Yes.

drop table a1
go
create table a1
(
col1 int,
col2 int,
PRIMARY KEY (Col1, Col2)
)

go

create table a2
(
 myid int not null,
 col1 int not null,
 col2 int not null,
 FOREIGN KEY (col1, col2) REFERENCES a1 (col1, col2)

)

go
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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

618 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