如何在 PostgreSQL 中定义运算符别名?

发布于 2024-09-14 06:25:33 字数 364 浏览 1 评论 0原文

有没有一种简单的方法可以在 PostgreSQL 中为 = 运算符定义运算符别名?

对于 !=<> 运算符来说,这个问题是如何解决的? pg_operators 中似乎只有 <> 运算符。 != 运算符是硬编码的吗?

这对于使用自定义运算符的应用程序是必需的。在大多数环境中,此运算符的行为应类似于 =,但在某些情况下,我们通过创建自己的运算符和运算符类来定义特殊行为。但对于正常情况,我们的运算符应该只是 = 运算符的别名,这样对于使用哪个实现的应用程序来说是透明的。

Is there an easy way to define an operator alias for the = operator in PostgreSQL?

How is that solved for the != and <> operator? Only the <> operator seems to be in pg_operators. Is the != operator hard-coded?

This is needed for an application which uses a self-defined operator. In most environments this operator should act like a =, but there are some cases where we define a special behavior by creating an own operator and operator class. But for the normal case our operator should just be an alias for the = operator, so that it is transparent to the application which implementation is used.

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

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

发布评论

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

评论(1

乖不如嘢 2024-09-21 06:25:33

只需检查 pgAdmin,模式 pg_catalog。它包含所有运算符,并向您展示如何为所有数据类型创建它们。是的,您必须为所有数据类型创建它们。所以这不仅仅是一个“别名”,你需要很多别名。

char = char 的示例,使用!!!!作为别名:

CREATE OPERATOR !!!!   -- name
(
  PROCEDURE = pg_catalog.chareq,
  LEFTARG = "char",
  RIGHTARG = "char",
  COMMUTATOR = !!!!, -- the same as the name
  RESTRICT = eqsel,
  JOIN = eqjoinsel,
  HASHES,
  MERGES
);
SELECT 'a' !!!! 'a' -- true
SELECT 'a' !!!! 'b' -- false

同时查看手册并注意对于命名规则来说,它有一些限制。

Just check pgAdmin, the schema pg_catalog. It has all the operators and show you how the create them for all datatypes. Yes, you have to create them for all datatypes. So it's not just a single "alias", you need a lot of aliasses.

Example for a char = char, using !!!! as the alias:

CREATE OPERATOR !!!!   -- name
(
  PROCEDURE = pg_catalog.chareq,
  LEFTARG = "char",
  RIGHTARG = "char",
  COMMUTATOR = !!!!, -- the same as the name
  RESTRICT = eqsel,
  JOIN = eqjoinsel,
  HASHES,
  MERGES
);
SELECT 'a' !!!! 'a' -- true
SELECT 'a' !!!! 'b' -- false

Check the manual as well and pay attention to the naming rules, it has some restrictions.

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