【问题标题】:MySQL Autoincrement InnoDB like MyISAMMySQL Autoincrement InnoDB 像 MyISAM
【发布时间】:2011-09-17 05:03:07
【问题描述】:

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 中使用它时 - 不起作用。是否有解决方法(因为我需要交易)?

谢谢。

可能是从 cmets 到 Manual of MySQL 的更好示例 由 [name 保留] 于 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 在主键是复合时的功能。如果是这样的话,不——这样的功能不适用于 InnoDB。解决方法 - 放弃 auto_increment,编写自己的程序来计算密钥 - 基本上,很麻烦。

标签: mysql innodb myisam auto-increment


【解决方案1】:

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

您可以使用advisory locks 和触发器来解决它。

看到这个identical question for PostgreSQL。您需要编写相同的 MySQL 版本。

【讨论】:

  • 谢谢,但我需要将事务插入到近 20 个表中(xml 相邻映射) - 所以我需要尽可能简单地进行事务管理。
  • 好吧,更简单的方法是根据需要锁定您的表。但它会更慢。
【解决方案2】:

这将起作用:

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 更复杂的数据模型。

【讨论】:

  • 我发现它在 innoDB 中对我有用,只要我将自动递增字段作为主键的第一部分。
猜你喜欢
  • 2011-05-14
  • 2012-03-01
  • 2011-07-25
  • 1970-01-01
  • 2012-01-13
  • 1970-01-01
  • 2012-10-20
  • 2011-04-18
  • 2015-07-18
相关资源
最近更新 更多