MySQL 自动增量 InnoDB 类似 MyISAM

发布于 2024-11-15 19:59:56 字数 1612 浏览 3 评论 0原文

MyISAM 允许非常方便地创建连续剧。 例如,在表中主键是 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

所以逻辑是当 id 保持不变直到出现重复键时,在这种情况下(MyISAM)将递增 id。

但是当我尝试在 InnoDB 中使用它时 - 不起作用。有解决方法吗(因为我需要交易)?

谢谢。

可能是对 MySQL 手册的评论中更好的例子 由 [name notheld] 发布于 2003 年 10 月 23 日晚上 8:41

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, place)
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 |
+----+-----------+----------+-------+

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, place)
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 |
+----+-----------+----------+-------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不知在何时 2024-11-22 19:59:56

但是当我尝试在 InnoDB 中使用它时 - 不起作用。有解决方法吗(因为我需要交易)?

您可以使用 咨询锁 和触发器。

请参阅有关 PostgreSQL 的相同问题 。您将需要编写相同的 MySQL 版本。

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.

浪荡不羁 2024-11-22 19:59:56

这会起作用:

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

但这不会:

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

因为:

MyISAM的缺点

没有数据完整性(例如关系约束)检查,这将成为数据库管理员和应用程序开发人员的责任和开销。

InnoDB的缺点

由于InnoDB要处理表之间的不同关系,数据库管理员和方案创建者必须花费更多时间来设计比MyISAM更复杂的数据模型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文