创建sqlite索引时COLLATE关键字有什么作用?

发布于 2024-07-18 08:20:24 字数 339 浏览 1 评论 0原文

根据 sqlite3 文档

每个后面的 COLLATE 子句 列名定义排序规则 用于其中文本条目的序列 柱子。 默认整理顺序 是为以下内容定义的整理顺序 CREATE TABLE 中的该列 陈述。 或者如果没有整理顺序 另有定义时,内置 使用 BINARY 整理序列。

整理序列的作用是什么?什么是 BINARY 整理序列?

According to the sqlite3 documentation,

The COLLATE clause following each
column name defines a collating
sequence used for text entries in that
column. The default collating sequence
is the collating sequence defined for
that column in the CREATE TABLE
statement. Or if no collating sequence
is otherwise defined, the built-in
BINARY collating sequence is used.

What does a collating sequence do, and what is a BINARY collating sequence?

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

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

发布评论

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

评论(3

梦罢 2024-07-25 08:20:24

这是sql引擎内部对数据进行排序的方式。 二进制排序规则正如其所暗示的那样,它进行二进制比较。 一般来说,它是最快的排序规则,尽管我从未量化过它,因为它检查位模式,这意味着它区分大小写和重音。

It is the way that the sql engine orders data internally. Binary Collation does what it suggests, it does a binary comparison. Generally its the fastest collation though I have never quantified it, as it checks bit patterns, which means it is case and accent sensitive.

背叛残局 2024-07-25 08:20:24

二进制排序规则逐字节比较字符串,就像在 unicode 表中一样。 例如:A、B、a、b。
不区分大小写 (NOCASE) 的顺序为:a,A,b,B。

二进制排序规则的优点是速度快,因为字符串比较非常简单/快速。 在一般情况下,二进制索引可能不会产生预期的排序结果; 然而,对于精确匹配,它们可能很有用。

COLLATE NOCASE 也会影响区分大小写的查询。

如果您有一列包含以下值:“aa”、“aA”

select * from table where col = 'aa'

如果您使用 COLLATE NOCASE 创建列,它将返回“aa”和“aA”。 否则,如果您没有指定它,它将仅返回“aa”。

您还可以在查询中指定它(这比您使用 COLLATE NOCASE 创建列要慢)

select * from table where col = 'aa' COLLATE NOCASE

Binary collation compares your string byte by byte, as in an unicode table. For example: A,B,a,b.
A case insensitive (NOCASE) order would be: a,A,b,B.

The advantage of binary collation is its speed, as string comparison is very simple/fast. In the general case, indexes with binary might not produce expected results for sort; however, for exact matches they can be useful.

COLLATE NOCASE also affects case sensitive queries.

If you have a column with these values: 'aa', 'aA'

select * from table where col = 'aa'

If you have created your column with COLLATE NOCASE it will return both 'aa' and 'aA'. Otherwise, if you didn't specify it, it will return only 'aa'.

You can also specify it in a query (this is slower then if you had created your column with COLLATE NOCASE)

select * from table where col = 'aa' COLLATE NOCASE
怪我鬧 2024-07-25 08:20:24

排序规则不仅影响索引或结果的排序,还可能影响表上某些操作的有效性。 因此,您可能会发现将不区分大小写的文本列定义为主键(或使用 UNIQUE 约束)是合适的,如下所示:

CREATE TABLE t1 (
  id TEXT COLLATE NOCASE PRIMARY KEY
);

插入 id' 后a'

INSERT INTO t1 VALUES ('a');

无法添​​加另一个值为 'A'id(仅因大小写而异)。 以下 SQL

INSERT INTO t1 VALUES ('A');

将失败并显示

运行时错误:UNIQUE 约束失败:t1.id (19)

尽管如此,仍然可以将id值从小写更改为大写:

UPDATE t1 SET id = 'A' WHERE id = 'A';

...这非常有趣,因为where子句实际上工作时不区分大小写,并且从类似 A 的内容更改为正确的 A

Collation does not only affect ordering in indexes or results, but may also affect the validity of certain operations on tables. So you might find it appropriate to define a case-insensitive text column as primary key (or with UNIQUE constraint), like this:

CREATE TABLE t1 (
  id TEXT COLLATE NOCASE PRIMARY KEY
);

After inserting an id value 'a',

INSERT INTO t1 VALUES ('a');

it's not possible to add another id with value 'A' (differing just by case). The following SQL

INSERT INTO t1 VALUES ('A');

will fail with

Runtime error: UNIQUE constraint failed: t1.id (19)

It's nevertheless possible to change the id value from lowercase to uppercase:

UPDATE t1 SET id = 'A' WHERE id = 'A';

… which is quite interesting because the where clause actually works case-insensitively and the change is made from something A-like to a proper A.

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