If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Problem with auto increment

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-15-05, 01:16
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Problem with auto increment

Hi,

I'm seeing a rather strange behaviour with auto increment in two of the tables I've created. These tables have the following specs:

Code:
CREATE TABLE table_myisam (
  id mediumint(8) unsigned NOT NULL auto_increment,
) TYPE=MyISAM;

CREATE TABLE table_innodb (
  id mediumint(8) unsigned NOT NULL auto_increment,
) TYPE=InnoDB;
I've a perl script to interact with the database.The desired behaviour is as follows:

When I insert in entry into table_myisam, a corresponding entry is also inserted into table_innodb. These two entries have the same id's i.e. if the entry in table_myisam is inserted with an entry of id 28, table_innodb will also be inserted with an entry of the same id.

Similarly, when an entry is deleted from table_myisam, a corresponding entry is deleted from table_innodb.

After some time, both tables should look as follows:

table_myisam
id
2
5
10
11

table_innodb
id
2
5
10
11

However, on two occasions, an entry was deleted from table_myisam so that the two tables appeared as follows:

table_myisam
id
2
5
10

table_innodb
id
2
5
10

That is, entry 11 was deleted and everything was okay.

This was followed by a new insertion. What happened was that while table_myisam was inserted with the next value after 11, table_innodb was inserted with the deleted value 11:

table_myisam
id
2
5
10
12

table_innodb
id
2
5
10
11

What could have gone wrong? Isn't table_innodb supposed to be inserted with the auto increment value 12?

I'm at my wit's end and I hope someone can enlighten me.

Thanks in anticipation

Last edited by pearl2; 02-15-05 at 01:20.
Reply With Quote
  #2 (permalink)  
Old 02-15-05, 04:19
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
the purpose of auto_increment columns is to provide a number that is unique

it does this by incrementing

it does not guarantee that previously used numbers won't be re-used, just that they will be unique

it seems that both tables are doing this, even if not exactly in identical manner

if you need the exact same number in both tables, you should copy the number from one to the other
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-15-05, 05:57
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Ah, I see.

Great thanks, r937
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On