Corbex
Corbex

Reputation: 11

Unable to create Foreign Key

There are two tables:

I try to make Class.CourseID FK to Course.CourseID but I get this message:

The columns in table 'Course' do not match an existing primary key or UNIQUE constraint.

What would I do?

Upvotes: 1

Views: 343

Answers (2)

deha
deha

Reputation: 815

The "structure" of FK must be the same as PK - two fields. With only CourseID as a FK you can't distinguish between records.

Example: Let c1, c2 be Course records, d1 a class record:

c1: {1, 1}
c2: {1, 2}

Now assume d1 record to be sth like {1, 1} -> you meant c1 or c2?

You should make ONLY CourseID PK or make a complex FK (CourseID, DepartmentID) or create a surrogate PK in Course table. Personally, I would go for 1st or 3rd solution

Upvotes: 0

Peter
Peter

Reputation: 408

DepartmentID should not be part of the primary key of table Course. Or if you need it like that, then restrucutre the FK in the Class table so it will reference both (so the real primary key of table Course).

Upvotes: 1

Related Questions