Page 1 of 1

Use of Primary Keys

Posted: Sat May 13, 2006 7:22 am
by GeXus
Hello,

I have a DB that I created for managing banners.. I have two tables... 1 for publisher information and the other for the tracking information.

I have it set so PublisherID is the primary key for both tables...

How do I replicate it so that the IDs are always matching? OR when I create my publisher, do I just make sure when I make my inserts into the tracking table that I insert using the same ID?

I guess what I dont think I understand is what is the real reason for having these as primary keys, if there is one, will it just make indexing faster when I join the two?

Re: Use of Primary Keys

Posted: Sat May 13, 2006 9:12 am
by timvw
GeXus wrote: How do I replicate it so that the IDs are always matching? OR when I create my publisher, do I just make sure when I make my inserts into the tracking table that I insert using the same ID?
- Start a transaction
- Insert in the first table (generating the id is easer with auto_increment)
- Get the generated id
- Insert into the second table (use the id as a value for primary key)
- Commit transaction
GeXus wrote: I guess what I dont think I understand is what is the real reason for having these as primary keys, if there is one, will it just make indexing faster when I join the two?
I would add a foreign key constraint on the second table. And yes, an index on that foreign key will probably make it easier for the querysolver...