Solved

Composite Primary Key

Posted on 2003-11-10
3
1,578 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 30 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

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

Suggested Solutions

Title # Comments Views Activity
Table where row act as column 11 71
Finding Where Clause Value in SQL Views and SP 21 43
SQL Syntax 6 32
A question about syntax 5 27
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

726 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