开发者

MySQL Autoincrement InnoDB like MyISAM

MyISAM allows very convenient way to create serials. E.g. In the table primary key is id+seq(-uence)

id seq
1  1  insert into table(seq) values(1),(2),(3),(1),(2),(1),(1),(2);
1  2
1  3
2  1
2  2
3  1
4  1
4  2

So logic is when id remains the same untill appears duplicate key, in this case (MyISAM) will increment id.

But when I try use it in InnoDB - doesn't work. Is there a workaround (because I need transactions)?

Thanks.

May be better example from comments to Manual of MySQL Posted by [name withheld] on October 23 2003 8:41pm

create table location
(
    id bigint not null auto_increment, -- "serial" per 4.1
    longitude int,
    latitude int,
    place int,
    primary key(id, longitude, latitude, place)
);

insert into location (longitude, latitude, place)
values (0,0,0), (1,1,1), (2,2,2);

select * from foo;

+----+-----------+----------+-------+
| id | longitude | latitude | place |
+----+-----------+----------+-------+
|  1 |         0 |        0 |     0 |
|  2 |         1 |        1 |     1 |
|  3 |         2 |        2 |     2 |
+----+-----------+----------+-------+


drop table location;

create table location
(
    id bigint not null auto_increment, -- "serial" per 4.1
    longitude int,
    latitude int,
    place int,
    primary key(longitude, latitude, place, id)
);

insert into location (longitude, latitude, plac开发者_运维百科e)
values (0,0,0), (1,1,1), (2,2,2), (0,0,0);

select * from location order by id;

+----+-----------+----------+-------+
| id | longitude | latitude | place |
+----+-----------+----------+-------+
|  1 |         0 |        0 |     0 |
|  1 |         1 |        1 |     1 |
|  1 |         2 |        2 |     2 |
|  2 |         0 |        0 |     0 |
+----+-----------+----------+-------+


But when I try use it in InnoDB - doesn't work. Is there a workaround (because I need transactions)?

You can work around it using advisory locks and triggers.

See this identical question for PostgreSQL. You'll want to write the MySQL version of the same.


This will work:

create table location
(
    id bigint not null auto_increment,
    longitude int,
    latitude int,
    place int,
    primary key(longitude, latitude, place, id)
) ENGINE =myisam;

But this will not:

create table location
(
    id bigint not null auto_increment,
    longitude int,
    latitude int,
    place int,
    primary key(longitude, latitude, place, id)
) ENGINE =innodb;

because:

Disadvantages of MyISAM

No data integrity (e.g. relationship constraints) check, which then comes a responsibility and overhead of the database administrators and application developers.

and

Disadvantages of InnoDB

Because InnoDB has to take care of the different relationships between tables, database administrator and scheme creators have to take more time in designing the data models which are more complex than those of MyISAM.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜