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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.