d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Composite Key Question > Newb Here
Add Reply New Topic New Poll
Member
Posts: 3,466
Joined: Jul 22 2012
Gold: 30,884.00
Feb 6 2019 01:36am
A composite key is a combination of two or more columns in a table that can be used to uniquely identify each row in the table.


But what happens if both columns have the same value. Can it still uniquely identify the row in the table. Would I need to choose a 3rd column for the composite key. Or is it better to make a primary key that that can just uniquely identify the row.

Ex. a student retaking the same course again because he fail.

Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Feb 6 2019 02:28am
When would both columns have the same value? If that happens, it seems like your design is faulty in the first place.
Member
Posts: 3,466
Joined: Jul 22 2012
Gold: 30,884.00
Feb 6 2019 02:36am
Quote (Klexmoo @ Feb 6 2019 01:28am)
When would both columns have the same value? If that happens, it seems like your design is faulty in the first place.




STU_ID AND COURSE_ID is the composite key. If a student fail last year and register again for this year, there would be same entry again.
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Feb 6 2019 02:50am
Quote (RYce @ 6 Feb 2019 09:36)
https://i.imgur.com/RhmsfPH.png

STU_ID AND COURSE_ID is the composite key. If a student fail last year and register again for this year, there would be same entry again.


Right, then you do need all three columns as your composite key.
Member
Posts: 3,466
Joined: Jul 22 2012
Gold: 30,884.00
Feb 6 2019 03:00am
in this case, would it be better to just use a surrogate key.

wouldn't it make referencing to another table easier.
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Feb 6 2019 04:37am
Quote (RYce @ 6 Feb 2019 10:00)
in this case, would it be better to just use a surrogate key.

wouldn't it make referencing to another table easier.


Surrogate keys make it such that your database is no longer normalized (because the key is not actually part of the data, you can therefore have effectively duplicate rows).

Composite keys make it such that this is not possible, keeping your database in normal form.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll