[H2 和 Firebird]auto_increment、恒等还是触发器加生成器?
在 Firebird 中,拥有自动增量列的唯一方法是设置一个生成器并将其与触发器一起使用。
在 H2 数据库中,有 auto_increment 和identity 关键字来执行此操作。
最好的方法是什么?
Mysql也使用auto_increment,但是并发读和表锁有一些问题,不是吗?
谢谢。
in Firebird the only way to have an auto increment coloumn is to setup a generator and use it with a trigger.
In H2 db there are auto_increment and identity keywords to do it.
What is the best way?
Mysql use auto_increment too, but there is some problems with cuncurrence read and table lock, isn't true?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不会,不存在并发读和表锁的问题。
使用每个 RDBMS 原生的伪键功能的优点是 RDBMS 引擎自动处理 id 值的分配,因此并发客户端不会分配相同的 id 值。它只需要对内部 id 计数器进行短暂锁定,而不需要对整个表进行锁定。
什么是最好的方法并不重要。您应该使用您正在使用的 RDBMS 提供的功能。不幸的是,直到 SQL:2003,伪键才在 ANSI SQL 标准中定义。到那时,每个供应商都创建了自己的专有功能和语法。
SEQUENCE
对象,这些对象有点像 FirebirdGENERATOR
或SEQUENCE
对象。IDENTITY
作为列选项。AUTO_INCRMENT
列选项,并且还支持伪数据类型SERIAL
以使其与 PostgreSQL 具有一定的交叉兼容性。如果 H2 同时支持
IDENTITY
和AUTO_INCRMENT
,那么可能会让从 MySQL 或 Microsoft SQL Server 迁移的人们更加熟悉它。我不知道 H2,但我猜这两种语法形式都访问相同的内部功能。我找到了 H2 的文档。看来
IDENTITY
和AUTO_INCRMENT
都使用序列
。No, there's no problem of concurrent read and table lock.
The advantage of using the pseudokey feature native to each RDBMS is that the RDBMS engine handles allocation of id values atomically, so concurrent clients will not allocate the same id value. It only needs a brief lock on the internal id counter, not any whole table.
What is the best way is immaterial. You should use the feature provided by the RDBMS you're using. Unfortunately, pseudokeys weren't defined in the ANSI SQL standard until SQL:2003. By that time, each vendor had created their own proprietary feature and syntax.
SEQUENCE
objects which are sort of like FirebirdGENERATOR
orSEQUENCE
objects.IDENTITY
as a column option.AUTO_INCREMENT
column option, and also supports a pseudo data typeSERIAL
to give it some cross-compatibility with PostgreSQL.If H2 supports both
IDENTITY
andAUTO_INCREMENT
, it's probably to make it more familiar for people migrating from either MySQL or Microsoft SQL Server. I don't know H2, but I would guess both syntax forms access the same internal feature.I found the docs for H2. It appears both
IDENTITY
andAUTO_INCREMENT
make use of aSEQUENCE
.