Multiple foreign key on many-to-many table

I'm attempting to create a many-to-many relationship in SQL Server 2012.  Below is the code I'm using, but I'm receiving an error.  How do you add multiple foreign keys to the same table?  Or, is there a different way to do a many-to-many relationship?

USE Ordering

CREATE TABLE ParentDepartment
(
	ParentDepartmentID Int NOT NULL IDENTITY (1,1),
	DepartmentCode Int,
	Name Nvarchar(100)
	PRIMARY KEY(ParentDepartmentID)
)

CREATE TABLE ChildDepartment
(
	ChildDepartmentID Int NOT NULL IDENTITY (1,1),
	LowOrgCode Int,
	Name Nvarchar(100)
	PRIMARY KEY(ChildDepartmentID)
)

CREATE TABLE ParentChildDepartment
(
	ParentChildDepartmentID Int NOT NULL IDENTITY (1,1),
	ParentDepartmentID int,
	ChildDepartmentID int
	PRIMARY KEY(ParentChildDepartmentID)
	CONSTRAINT fk_ParentID FOREIGN KEY(ParentDepartmentID) REFERENCES ParentDepartment(ParentDepartmentID)
	CONSTRAINT fk_ChildID FOREIGN KEY(ChildDepartmentID) REFERENCES ChildDepartment(ChildDepartmentID)
)

Open in new window

bobjones1208Asked:
Who is Participating?
 
F PCommented:
Try writing it like this:


CREATE TABLE ParentChildDepartment
(
    ParentDepartmentID int,
    ChildDepartmentID int,
    CONSTRAINT parent_child_pk PRIMARY KEY (ParentDepartmentID, ChildDepartmentID),
    CONSTRAINT FK_ChildDepartmentID 
        FOREIGN KEY (ChildDepartmentID) REFERENCES ChildDepartment(ChildDepartmentID),
    CONSTRAINT FK_ParentDepartmentID 
        FOREIGN KEY (ParentDepartmentID) REFERENCES ParentDepartment(ParentDepartmentID)
);

Open in new window

0
 
F PCommented:
Are you putting ; at the end of each statement? - So you're not running 3 statements in a single query without terminating each?
0
 
F PCommented:
... also, what error are you getting? Can you please post it?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
bobjones1208Author Commented:
I didn't include the semi colon, but it didn't give me an error.  I tried adding it and I get the same error.

It's saying "Multiple FOREIGNKEY constraints were specified for column "ChildDepartmentID", table "ParentChildDepartment"
0
 
bobjones1208Author Commented:
That worked.  So, just for clarification - I have to combine the two ints to create the primary key instead of using an auto increment value?
0
 
F PCommented:
You specify the columns as a joint primary key, like a unique index using MySQL, making them unique and constrain their properties that way. Try setting SHOWPLAN_ALL on and it should give you more insight into what's going on with the query:

https://msdn.microsoft.com/en-us/library/ms180765(v=sql.105).aspx
0
 
bobjones1208Author Commented:
Very informative.
0
 
F PCommented:
You're also missing commas.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.