Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primary key question

Is there a benefit to having a single column primary key vs a composite primary key?

I have a table that consists of two id columns which together make up the primary key.

Are there any disadvantages to this? Is there a compelling reason for me to throw in a third column that would be unique on it's own?

like image 233
Abe Miessler Avatar asked Nov 30 '22 10:11

Abe Miessler


2 Answers

Database Normalization nuts will tell you one thing.

I'm just going to offer my own opinion of what i've learned over the years..I stick an AutoIncrementing ID field to Every ($&(@#$)# one of my tables. It makes life a million times easier in the long run to be able to single out with impunity a single row.

This is from a "down in the trenches" developer.

like image 178
Caladain Avatar answered Dec 04 '22 08:12

Caladain


Single column keys are simple to write, simple to maintain, and simple to understand.

If you're going to have a huge number of rows - billions? - maybe saving a byte here and there will help.

But if you're not looking at extreme cases, optimizing for "simple" is often the best way to go.

like image 35
Dean J Avatar answered Dec 04 '22 06:12

Dean J