Firebird 配置 - 关闭区分大小写

发布于 2024-09-26 05:08:10 字数 121 浏览 7 评论 0原文

我希望在 Firebird 数据库中执行不区分大小写的搜索,而不修改实际查询。换句话说,我希望所有现有的“SELECT/WHERE/LIKE”语句都能检索 BOB、Bob 和 bob。 Firebird 配置是否允许修改此行为?

I'm looking to perform case-insensitive search in a Firebird database, without modifying actual queries. In other words, I'd like all my existing "SELECT/WHERE/LIKE" statements to retrieve BOB, Bob, and bob. Does Firebird configuration allow to modify this behavior?

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

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

发布评论

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

评论(3

当梦初醒 2024-10-03 05:08:10

尝试使用类似的东西:
想象一下,您有一个像这样的人员表:

CREATE TABLE PERSONS (
  PERS_ID INTEGER NOT NULL PRIMARY KEY,
  LAST_NAME VARCHAR(50),
  FIRST_NAME VARCHAR(50)
);

现在有一个应用程序,它允许用户按姓氏和/或名字搜索人员。因此,用户输入他正在搜索的人的姓氏。

我们希望这个搜索不区分大小写,即无论用户输入“Presley”、“presley”、“PRESLEY”,甚至“PrESley”,我们总是想找到国王。

啊,是的,我们希望搜索速度能够加快。所以必须有一个索引来加速它。

进行不区分大小写的比较的一种简单方法是将两个字符串都大写,然后比较两个字符串的大写版本。

大写有限制,因为有些字母不能大写。另请注意,有些语言/脚本没有大小写之类的东西。因此,本文中描述的技术最适合欧洲语言。

为了获得真正完美的结果,需要一种不区分大小写(CI)和/或不区分重音(AI)的排序规则。然而,在撰写本文时(2006 年 7 月),Firebird 2.0 只有两个捷克语 AI/CI 排序规则。随着时间的推移,这种情况有望得到改善。

(您应该了解字符集和排序规则的概念,以便理解接下来的内容。我在示例中使用 DE_DE 排序规则,这是 ISO8859_1< 中德语/德国的排序规则/code> 字符集。)

为了从 Firebird 内置的 UPPER() 函数获得正确的结果,您必须指定排序规则。这可以在表的 DDL 定义中:

CREATE TABLE PERSONS (
  PERS_ID INTEGER NOT NULL PRIMARY KEY,
  LAST_NAME VARCHAR(50) COLLATE DE_DE,
  FIRST_NAME VARCHAR(50) COLLATE DE_DE
);

或者可以在调用 UPPER() 函数时完成:

SELECT UPPER (LAST_NAME COLLATE DE_DE) FROM PERSONS;

http://www.destructor.de/firebird/caseinsensitivesearch.htm

或者您可以编辑查询并添加 lower() 函数

LOWER()

适用于: DSQL、ESQL、PSQL

添加于:2.0

说明:返回输入字符串的小写等效项。即使使用默认(二进制)排序规则,此函数也可以正确地小写非 ASCII 字符。但字符集必须适当:例如,对于 ASCII 或 NONE,只有 ASCII 字符是小写的;对于 OCTETS,整个字符串将原样返回。

结果类型:VAR(CHAR)

语法:

LOWER (str)

重要

如果在数据库中声明了外部函数LOWER,它将混淆内部函数。要使内部函数可用,请删除或更改外部函数 (UDF)。

示例:

select field from table
  where lower(Name) = 'bob'

http://www.firebirdsql.org/refdocs/langrefupd21-intfunc-lower .html

Try using something like:
Imagine you have a table of persons like this one:

CREATE TABLE PERSONS (
  PERS_ID INTEGER NOT NULL PRIMARY KEY,
  LAST_NAME VARCHAR(50),
  FIRST_NAME VARCHAR(50)
);

Now there is an application, which allows the user to search for persons by last name and/or first name. So the user inputs the last name of the person he is searching for.

We want this search to be case insensitive, i.e. no matter if the user enters "Presley", "presley", "PRESLEY", or even "PrESley", we always want to find the King.

Ah yes, and we want that search to be fast, please. So there must be an index speeding it up.

A simple way to do case insensitive comparisons is to uppercase both strings and then compare the uppercased versions of both strings.

Uppercasing has limitations, because some letters cannot be uppercased. Note also that there are languages/scripts where there is no such thing as case. So the technique described in this article will work best for European languages.

In order to get really perfect results one would need a case insensitive (CI) and/or accent insensitive (AI) collation. However, at the time of this writing (July 2006) there are only two Czech AI/CI collations for Firebird 2.0. The situation will hopefully improve over time.

(You should know the concepts of Character Sets and Collations in order to understand what comes next. I use the DE_DE collation in my examples, this is the collation for German/Germany in the ISO8859_1 character set.)

In order to get correct results from the UPPER() function that is built into Firebird, you must specify a collation. This can be in the DDL definition of the table:

CREATE TABLE PERSONS (
  PERS_ID INTEGER NOT NULL PRIMARY KEY,
  LAST_NAME VARCHAR(50) COLLATE DE_DE,
  FIRST_NAME VARCHAR(50) COLLATE DE_DE
);

or it can be done when calling the UPPER() function:

SELECT UPPER (LAST_NAME COLLATE DE_DE) FROM PERSONS;

http://www.destructor.de/firebird/caseinsensitivesearch.htm

or you can edit your queries and add the lower() function

LOWER()

Available in: DSQL, ESQL, PSQL

Added in: 2.0

Description: Returns the lower-case equivalent of the input string. This function also correctly lowercases non-ASCII characters, even if the default (binary) collation is used. The character set must be appropriate though: with ASCII or NONE for instance, only ASCII characters are lowercased; with OCTETS, the entire string is returned unchanged.

Result type: VAR(CHAR)

Syntax:

LOWER (str)

Important

If the external function LOWER is declared in your database, it will obfuscate the internal function. To make the internal function available, DROP or ALTER the external function (UDF).

Example:

select field from table
  where lower(Name) = 'bob'

http://www.firebirdsql.org/refdocs/langrefupd21-intfunc-lower.html

人生百味 2024-10-03 05:08:10

死灵术。
您可以指定排序规则,而不是小写字段:

SELECT field FROM table
WHERE Name = 'bob' COLLATE UNICODE_CI

请参阅
https://firebirdsql.org/refdocs/langrefupd21-collat​​ions.html

也许你可以还将其指定为数据库的默认字符集,请参阅
http://www.destructor.de/firebird/charsets.htm

Necromancing.
Instead of a lowercase-field, you can specify the collation:

SELECT field FROM table
WHERE Name = 'bob' COLLATE UNICODE_CI

See
https://firebirdsql.org/refdocs/langrefupd21-collations.html

Maybe you can also specify it as the default character-set for the database, see
http://www.destructor.de/firebird/charsets.htm

谈场末日恋爱 2024-10-03 05:08:10

最终,我创建了包含小写版本的必填字段的影子列。

Eventually I went with creating shadow columns containing lower-cased versions of required fields.

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