Solved

Composite Primary Key

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

707 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

14 Experts available now in Live!

Get 1:1 Help Now