• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1582
  • Last Modified:

Composite Primary Key

How can i implement :
Foreign key of one table referencing composite primary key of other table?
0
ewhopcis
Asked:
ewhopcis
  • 2
1 Solution
 
namasi_navaretnamCommented:
Try this

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


id1, id2 columns exists in both Table1, Table2
0
 
ewhopcisAuthor Commented:
can i define the composite foreign key while creating the table?
0
 
namasi_navaretnamCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now