MySQL 自动增量 InnoDB 类似 MyISAM
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 咨询锁 和触发器。
请参阅有关 PostgreSQL 的相同问题 。您将需要编写相同的 MySQL 版本。
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:
But this will not:
because:
and