SQLTeam.com | Weblogs | Forums

Composite FK with One Primary Key



I have the following code;


ALTER TABLE [dbo].[Admin_Patient]  WITH CHECK ADD  CONSTRAINT [FK_Admin_Patient_Admin_Organisation] FOREIGN KEY([OrganisationGuid],[ExternalUsualGPOrganisation])
REFERENCES [dbo].[Admin_Organisation] ([OrganisationGuid])

ALTER TABLE [dbo].[Admin_Patient] CHECK CONSTRAINT [FK_Admin_Patient_Admin_Organisation]

I am trying to have one primary key (OrganisationGuid) from dbo.Admin_Organsiation with a relationship with 2 foreign keys OrganisationGuid,ExternalUsualGPOrganisation on table dbo.Admin_Patient

How can I achieve this ?

The above code give me an error -

Number of referencing columns in foreign key differs from number of referenced columns, table 'dbo.Admin_Patient'

Same error I receive when doing it via the wizard.



surrogate key


The foreign key must be guaranteed to be unique in the parent table; not just exist. The primary key would, of course, be unique but you'll need to define a unique key in the parent table with composite columns. Something along the lines of:CREATE UNIQUE INDEX UQ_MyTable on MyTable(MyPK, MyOtherColumn)


Yes, the number of keys must match. You need to specify either 1 or 2 keys on both tables to relate to each other.