Solved

Composite Primary Key

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

895 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

13 Experts available now in Live!

Get 1:1 Help Now