MySQL Forums
Forum List  »  Newbie

Re: Get Number and Increment
Posted by: Phillip Ward
Date: July 10, 2020 08:09AM

Quote

Yes, there is only 1 row in the table.

Good to hear.

You'd be surprised how many people pick up a piece of code like that and throw it against a million row table (by mistake, hopefully) and then wonder why it's so slow!

Quote

the field that is incremented is of type int. (Not an autoincrement). Does that make a difference ?

Not at all.

int is what it should be.

I was trying (and clearly failing; apologies) to point out that your use of the last_insert_id() function as part of your original update statemetn was incorrect.

You need that function when you insert, say, a new "Parent" record with an auto-increment key and need to re-use the newly-generated ID value of that "Parent" record to insert a related, "Child" record, something like this:

insert 
into parent ( name )                     /* No ID value supplied, so MySQL will generate one */ 
values ( 'Fred' ) 
; 

insert 
into child ( parent_id, name )           /* No ID value supplied, so MySQL will generate one, but only after ... */
values ( last_insert_id(), 'Pebbles' )   /* Use the ID just generated for Fred */
; 

select * 
from parent ; 

+----+------+
| id | name | 
+----+------+
|  6 | Fred | 
+----+------+

select  *
from child ; 

+----+-----------+---------+ 
| id | parent_id | name    | 
+----+-----------+---------+ 
| 77 |         6 | Pebbles |
+----+-----------+---------+


Regards, Phill W.

Options: ReplyQuote


Subject
Written By
Posted
July 09, 2020 04:53AM
Re: Get Number and Increment
July 10, 2020 08:09AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.