General information
Forum:
Microsoft SQL Server
Title:
Foreign Key Using Multiple Fields
I am trying to set up a foreign key between two tables A and B. Table A has a primary key called PKey CHAR(28). I am trying to take three fields from Table B (call them Field1 CHAR(4), Field2 CHAR(3), and Field3 CHAR(21)) and create a foreign key to Table A. I try to do this in the Diagram Tool and I keep getting errors because it is trying to match on the first field instead of all three (even though I have all three selected.)
IN Query Analyzer I use the Alter Table command below:
ALTER TABLE [dbo].[TableB] ADD
CONSTRAINT [FK_Greg] FOREIGN KEY
(
[Field1], [Field2], [Field3]
) REFERENCES [dbo].[TableA] (
[PKEY]
)
and get the following error:
Number of referencing columns in foreign key differs from number of referenced columns, table 'dbo.TableB'.
Can you tell me if what I am trying to do can be done and if so, how.
Thank You.
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only