[H2 和 Firebird]auto_increment、恒等还是触发器加生成器?

发布于 2024-08-31 07:25:48 字数 189 浏览 5 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(1

春庭雪 2024-09-07 07:25:48

不会,不存在并发读和表锁的问题。

使用每个 RDBMS 原生的伪键功能的优点是 RDBMS 引擎自动处理 id 值的分配,因此并发客户端不会分配相同的 id 值。它只需要对内部 id 计数器进行短暂锁定,而不需要对整个表进行锁定。

什么是最好的方法并不重要。您应该使用您正在使用的 RDBMS 提供的功能。不幸的是,直到 SQL:2003,伪键才在 ANSI SQL 标准中定义。到那时,每个供应商都创建了自己的专有功能和语法。

  • Oracle 使用 SEQUENCE 对象,这些对象有点像 Firebird GENERATORSEQUENCE 对象。
  • Microsoft SQL Server 使用IDENTITY 作为列选项。
  • IBM DB2 和 PostgreSQL 都支持序列,但它们也有一些声明性的魔法,可以使列隐式地从序列中获取值。
  • MySQL 使用 AUTO_INCRMENT 列选项,并且还支持伪数据类型 SERIAL 以使其与 PostgreSQL 具有一定的交叉兼容性。
  • SQLite 只是假设任何整数主键列都是自动递增的。

如果 H2 同时支持 IDENTITYAUTO_INCRMENT,那么可能会让从 MySQL 或 Microsoft SQL Server 迁移的人们更加熟悉它。我不知道 H2,但我猜这两种语法形式都访问相同的内部功能。

我找到了 H2 的文档。看来 IDENTITYAUTO_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.

  • Oracle uses SEQUENCE objects which are sort of like Firebird GENERATOR or SEQUENCE objects.
  • Microsoft SQL Server uses IDENTITY as a column option.
  • IBM DB2 and PostgreSQL both support sequences, but they also have some declarative magic to make columns take their values from sequences implicitly.
  • MySQL uses the AUTO_INCREMENT column option, and also supports a pseudo data type SERIAL to give it some cross-compatibility with PostgreSQL.
  • SQLite just assumes any integer primary key column is auto-incrementing.

If H2 supports both IDENTITY and AUTO_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 and AUTO_INCREMENT make use of a SEQUENCE.

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