diff --git a/include/myisam.h b/include/myisam.h index 94b5d23bba6..def8b492681 100644 --- a/include/myisam.h +++ b/include/myisam.h @@ -100,6 +100,7 @@ typedef struct st_mi_create_info ulong raid_chunksize; uint old_options; uint8 language; + my_bool with_auto_increment; } MI_CREATE_INFO; struct st_myisam_info; /* For referense */ diff --git a/myisam/mi_check.c b/myisam/mi_check.c index 67a7d6f363d..ca5c8f9ecb4 100644 --- a/myisam/mi_check.c +++ b/myisam/mi_check.c @@ -3694,8 +3694,8 @@ void mi_disable_non_unique_index(MI_INFO *info, ha_rows rows) MI_KEYDEF *key=share->keyinfo; for (i=0 ; i < share->base.keys ; i++,key++) { - if (!(key->flag & HA_NOSAME) && ! mi_too_big_key_for_sort(key,rows) && - info->s->base.auto_key != i+1) + if (!(key->flag & (HA_NOSAME|HA_AUTO_KEY)) && + ! mi_too_big_key_for_sort(key,rows)) { share->state.key_map&= ~ ((ulonglong) 1 << i); info->update|= HA_STATE_CHANGED; diff --git a/myisam/mi_create.c b/myisam/mi_create.c index 039066c5b37..9082c2b0d95 100644 --- a/myisam/mi_create.c +++ b/myisam/mi_create.c @@ -304,7 +304,7 @@ int mi_create(const char *name,uint keys,MI_KEYDEF *keydefs, if (keydef->flag & HA_BINARY_PACK_KEY) options|=HA_OPTION_PACK_KEYS; /* Using packed keys */ - if (keydef->flag & HA_AUTO_KEY) + if (keydef->flag & HA_AUTO_KEY && ci->with_auto_increment) share.base.auto_key=i+1; for (j=0, keyseg=keydef->seg ; j < keydef->keysegs ; j++, keyseg++) { diff --git a/mysql-test/r/auto_increment.result b/mysql-test/r/auto_increment.result index 66efd2ba567..e79e6aab56b 100644 --- a/mysql-test/r/auto_increment.result +++ b/mysql-test/r/auto_increment.result @@ -84,6 +84,16 @@ ordid ord 3 sdj 1 zzz drop table t1; +create table t1 (sid char(5), id int(2) NOT NULL auto_increment, key(sid, id)); +create table t2 (sid char(20), id int(2)); +insert into t2 values ('skr',NULL),('skr',NULL),('test',NULL); +insert into t1 select * from t2; +select * from t1; +sid id +skr 1 +skr 2 +test 1 +drop table t1,t2; create table t1 (a int not null primary key auto_increment); insert into t1 values (0); update t1 set a=0; diff --git a/mysql-test/t/auto_increment.test b/mysql-test/t/auto_increment.test index d86466572d8..5fba4bb9234 100644 --- a/mysql-test/t/auto_increment.test +++ b/mysql-test/t/auto_increment.test @@ -52,6 +52,13 @@ insert into t1 values (NULL,'sdj'),(NULL,'sdj'),(NULL,"abc"),(NULL,'abc'),(NULL, select * from t1; drop table t1; +create table t1 (sid char(5), id int(2) NOT NULL auto_increment, key(sid, id)); +create table t2 (sid char(20), id int(2)); +insert into t2 values ('skr',NULL),('skr',NULL),('test',NULL); +insert into t1 select * from t2; +select * from t1; +drop table t1,t2; + # # Test of auto_increment columns when they are set to 0 # @@ -62,3 +69,4 @@ update t1 set a=0; select * from t1; check table t1; drop table t1; + diff --git a/sql/ha_myisam.cc b/sql/ha_myisam.cc index 81b04cf5ba7..6e055f57c83 100644 --- a/sql/ha_myisam.cc +++ b/sql/ha_myisam.cc @@ -1021,7 +1021,7 @@ int ha_myisam::create(const char *name, register TABLE *table_arg, { int error; uint i,j,recpos,minpos,fieldpos,temp_length,length; - bool found_auto_increment=0; + bool found_auto_increment=0, found_real_auto_increment=0; enum ha_base_keytype type; char buff[FN_REFLEN]; KEY *pos; @@ -1091,11 +1091,11 @@ int ha_myisam::create(const char *name, register TABLE *table_arg, keydef[i].seg[j].null_bit=0; keydef[i].seg[j].null_pos=0; } - if (j == 0 && field->flags & AUTO_INCREMENT_FLAG && - !found_auto_increment) + if (field->flags & AUTO_INCREMENT_FLAG && !found_auto_increment) { keydef[i].flag|=HA_AUTO_KEY; found_auto_increment=1; + found_real_auto_increment=(j==0); } if (field->type() == FIELD_TYPE_BLOB) { @@ -1177,6 +1177,7 @@ int ha_myisam::create(const char *name, register TABLE *table_arg, bzero((char*) &create_info,sizeof(create_info)); create_info.max_rows=table_arg->max_rows; create_info.reloc_rows=table_arg->min_rows; + create_info.with_auto_increment=found_real_auto_increment; create_info.auto_increment=(info->auto_increment_value ? info->auto_increment_value -1 : (ulonglong) 0);