PDA

View Full Version : Dual Field Primary Key



Todd W
03-12-2008, 07:10 PM
Has anyone ever used a primary key that's made up of two fields?

I am using it because I want to utilize the "ON DUPLICATE KEY" ability MySQL provides for updating OR inserting a new row.
(I am doing this now and it works great, my concern is in the future when more gets added to the table.)

My concern is that the primary key is made up of the user_id and label_id. So, my concern is what if the user_id is 11 and the label_id is 22 that would make the primary key 1122. Now what if it was also checking a user_id of 1 and a label_id of 122 or any other way they can match up and over lap. Since they make up the SAME primary key will I run into issues or does mysql have a way to differentiate between them?


-Todd

rpanella
03-12-2008, 09:58 PM
That will not cause a problem. It does not create keys by concatenating the string, both fields are stored in whatever format you have set for that field.

For example if both user_id and label_id are INT (4 bytes) each key will store 8 bytes to uniquely identify that row.
________
Fetish latex (http://www.****tube.com/categories/521/latex/videos/1)

Todd W
03-13-2008, 12:58 AM
That will not cause a problem. It does not create keys by concatenating the string, both fields are stored in whatever format you have set for that field.

For example if both user_id and label_id are INT (4 bytes) each key will store 8 bytes to uniquely identify that row.

Awesome.
That's what I couldn't find on google (How it works).

FWIW both fields are INT(11), expecting some big #s ;)