Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-01-2007, 03:16 AM   PM User | #1
bowser1111
Regular Coder

 
Join Date: May 2006
Posts: 123
Thanks: 0
Thanked 0 Times in 0 Posts
bowser1111 is an unknown quantity at this point
Dependence on an Auto-Increment Primary Key

Guelphdad, this question is especially for you, since I have been following you along and reading your articles, but there's still something I'm not clear on.

Let's say you have a database for storing articles, and information related to those articles. Let's say I have an "articles" table (two fields, one is an auto PK "id", and the other is a text "content"). Now let's say there is a "links" table that contains links related to a given article (three fields, an auto PK "id", an int "article_id" for the article it refers to, and varchar "link").

1. This seems to have disaster written all over it. The reason is that these "auto_increment" fields are being referred to from outside the table. Wouldn't this mean that if you had gaps in the fields, made a dump of the table, and re-inserted all the information, suddenly the other tables would be pointing to the wrong articles? The "dump" scenario is only the most obvious, it feels like this could get messed up for plenty of other reasons.

2. I just want to confirm my understanding of your article, that the best way to run an INSERT into both tables would be (very simplified):

a. INSERT INTO articles (content) VALUES ("some content");
b. INSERT INTO links (article_id, link) VALUES (last_insert_id(), 'some link');

Does this look right?
bowser1111 is offline   Reply With Quote
Old 04-01-2007, 04:24 AM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
yes that is correct.

as for the database dump, don't worry. the data will not get changed. when you import back into a database none of the values get changed, even when an auto increment field is being loaded.

what might throw you off is that when you do not have an auto incremented field in your table and then add one, it gets automatically filled with values.

that doesn't happen on an import from a dump file though.
guelphdad is offline   Reply With Quote
Old 04-01-2007, 05:47 AM   PM User | #3
bowser1111
Regular Coder

 
Join Date: May 2006
Posts: 123
Thanks: 0
Thanked 0 Times in 0 Posts
bowser1111 is an unknown quantity at this point
So does that mean that data inside an auto_increment field is "safe" as long as you don't start changing it yourself?

Just to be clear, I plan on not only referring to this PK ID from other tables but also from the outside, such as displaying an "article number", and even having filenames based on them (i.e. "article_1.jpg", etc). Would this be acceptable as long as I make sure that I do not change the PK of the article table myself without changing everything else that refers to it?
bowser1111 is offline   Reply With Quote
Old 04-01-2007, 02:01 PM   PM User | #4
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
correct, don't change the values in your auto increment column. that is why I (and others) point out the numbers should never be changed when people are worried about gaps in the series of numbers.

often times you will see people with ids of
1,2,5,6,7 and they want to go back and insert a value for 4 which got deleted at some point.

The reason you shouldn't be doing that of course is exactly what you are questioning, a change here can mess up other parts of your application or sub tables etc.
guelphdad is offline   Reply With Quote
Old 04-01-2007, 05:29 PM   PM User | #5
bowser1111
Regular Coder

 
Join Date: May 2006
Posts: 123
Thanks: 0
Thanked 0 Times in 0 Posts
bowser1111 is an unknown quantity at this point
Ok, well I'll definitely be sure not to mess around with them then, but I guess I just want double assurance that they won't get messed up by anything else, beyond my control...

They won't right??

Please tell me they won't....
bowser1111 is offline   Reply With Quote
Old 04-01-2007, 07:04 PM   PM User | #6
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
They won't mess up if you don't mess them up!
guelphdad is offline   Reply With Quote
Old 04-02-2007, 01:13 AM   PM User | #7
bowser1111
Regular Coder

 
Join Date: May 2006
Posts: 123
Thanks: 0
Thanked 0 Times in 0 Posts
bowser1111 is an unknown quantity at this point
That's all I needed to know! Thanks a bunch.
bowser1111 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:37 AM.


Advertisement
Log in to turn off these ads.