可用版本:Dev (3.21) | 最新 (3.20) | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11

可更新的主键

适用于 ✅ 开源版   ✅ 专业版   ✅ 企业版

在大多数数据库设计指南中,主键值被期望永远不会改变 - 这是规范化数据库必不可少的假设。

与往常一样,这些规则也有例外,用户可能希望在可更新记录功能中允许可更新的主键值(注意:任何值都可以通过普通的更新语句更新)。一个例子

AuthorRecord author =
DSL.using(configuration) // This configuration will be attached to any record produced by the below query.
   .selectFrom(AUTHOR)
   .where(AUTHOR.ID.eq(1))
   .fetchOne();

author.setId(2);
author.store(); // The behaviour of this store call is governed by the updatablePrimaryKeys flag

上面的存储调用取决于 updatablePrimaryKeys 标志的值

  • false(默认值):由于假设主键的不可变性,存储调用将创建一个具有新主键值的新记录(副本)。
  • true:由于允许主键的可变性,存储调用会将主键值从 1 更改为 2

示例配置

Settings settings = new Settings()
    .withUpdatablePrimaryKeys(true); // Defaults to false

反馈

您对此页面有任何反馈吗? 我们很乐意听到您的反馈!

The jOOQ Logo