Question

I have read somewhere here that having primary key in each every table is a good thing to do... Let me say I have two tables "student" and "student_details" and i am using INNODB

"student" has a few columns like - student_id(Primary Key), student_name "student_details" has a few columns like - student_id(Foreign Key), Address, Phone, Mobile, etc..

Do "student_details" still need a primary key?

Was it helpful?

Solution

Whether you know it or not, what you are doing is column partitioning the table. You can have studentdetails.studentid be both a primary key and a foreign key. No problem with that. So, you can have a primary key in the table.

There are several reasons to do column partitioning, usually related to performance on commonly used columns or to create rows with more than the maximum number of columns. I doubt either of these apply in your case.

In fact, given the nature of the data, the studentdetails table is actually storing a "slowly-changing dimension". In simpler language, students move, so their address changes. Students change their telephone number. And so on. What you should really have is an effective and end date for each student details record. Then you can add an auto-incrementing primary key (which is what I would do) or you could declare studentdetails(studentid, effdate) as the primary key.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top