将 Oracle 表限制为单行的简单约束

发布于 2024-10-27 17:40:01 字数 244 浏览 2 评论 0原文

我正在寻找一种简单的方法来将应用程序配置表限制为 1 行。我知道我可以使用包含子查询的虚拟列或顶部的物化视图来完成此操作,但实际上这是一个简单的表,我想要一个约束,它不需要比创建表更多的行来实现。

我想到的最简单的方法是将 ID 字段设置为 1,并且检查约束 ID 必须为 1 - 但这会向表中添加一个令人困惑的额外字段,所以我希望有一些明显的我遗漏的东西,就像检查约束,其中 UNIQUE_FIELD = MAX(UNIQUE_FIELD),这是不允许的。

I'm looking for a simple way to limit an application configuration table to 1 row. I know I can do this with a virtual column containing a subquery, or a materialized view on top, but really it's a simple table I'd like a constraint that doesn't take more lines to implement than to create the table.

Simplest way I've thought of is an ID field set to 1, and a check constraint that the ID has to be 1 - but this adds a confusing extra field to the table so I'm hoping there's something obvious I'm missing, like a check constraint where UNIQUE_FIELD = MAX(UNIQUE_FIELD), which is not allowed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

煮茶煮酒煮时光 2024-11-03 17:40:01

最简单的是在常量上建立基于函数的唯一索引

> create unique index table_uk on one_row_table ('1');

替代方案:

您可以查看 DUAL,而不是表
这实际上意味着任何更新实际上都是创建或替换视图,这可能不合适。如果函数从包体或全局应用程序上下文返回值可能会导致无效问题,那么它可能是一种解决方法。

在 11g 中,可以选择只读表(或者,在早期版本中,只读表空间中的表)。

Simplest is a unique function-based index on a constant

> create unique index table_uk on one_row_table ('1');

Alternatives:

Rather than a table, you could have a view over DUAL
That would really mean any UPDATE would actually be a CREATE OR REPLACE VIEW which may not be appropriate. Functions returning values from a package body or global application contexts might be a workaround for that if it causes invalidation problems.

With 11g a READ ONLY table (or, in earlier versions, a table in a read only tablespace) is an option.

荒人说梦 2024-11-03 17:40:01

以下是一些选项:

  • 撤销对该表的 INSERT 权限。
  • 通过过程管理对配置表的所有访问
  • 将配置表替换为隐藏带有检查约束 (col=1) 和唯一约束的列的视图。
  • 无论如何都要创建该物化视图
  • 创建一个表会触发 INSERT 引发异常
  • 重新考虑表结构并添加 FROM_DATE,这样表就不会更新,但每次都会创建一个新行。您当前的配置将是 from_date 最高的行。该表的 current_config 视图很有用。
  • 将表替换为定义为 SELECT 1024 as buffer_size, '/var/tmp' as temp_dir, 'otherconstant' as other_constant from Dual 的视图。这不是一个固定的解决方案。

Here are some options:

  • Revoke the INSERT privileges on that table.
  • Manage all access to the configuration table through procedures
  • Replace the configuration table with a view that hides a column with a check constraint (col=1) and a unique constraint.
  • Create that materialized view anyway
  • Create a table trigger the fires on INSERT that throws an exception
  • Rethink the table structure and add a FROM_DATE, so that the table is NOT updated but you create a new row every time. Your current configuration will be the row with the highest from_date. A current_config view over that table is useful.
  • Replace the table with a view defined as SELECT 1024 as buffer_size, '/var/tmp' as temp_dir, 'other constant' as other_constant from dual. This isn't a solid solution.
你是暖光i 2024-11-03 17:40:01
CREATE TABLE Singleton
(x INTEGER NOT NULL UNIQUE CHECK (x = 1),
 col1 INTEGER NOT NULL);

(未经测试)

SQL 没有简洁的语法来将空属性集指定为键,这很遗憾,因为它将消除对冗余额外列的需要。这里的有效键实际上是空集,这就是为什么我使用 UNIQUE 约束而不是 PRIMARY KEY 作为对 x 的约束。

CREATE TABLE Singleton
(x INTEGER NOT NULL UNIQUE CHECK (x = 1),
 col1 INTEGER NOT NULL);

(untested)

SQL doesn't have a concise syntax to specify the empty set of attributes as a key, which is a shame because it would eliminate the need for the redundant extra column. The effective key here actually IS the empty set, which is why I've used a UNIQUE constraint instead of PRIMARY KEY for the constraint on x.

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