创建sqlite索引时COLLATE关键字有什么作用?
根据 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是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.
二进制排序规则逐字节比较字符串,就像在 unicode 表中一样。 例如:A、B、a、b。
不区分大小写 (NOCASE) 的顺序为:a,A,b,B。
二进制排序规则的优点是速度快,因为字符串比较非常简单/快速。 在一般情况下,二进制索引可能不会产生预期的排序结果; 然而,对于精确匹配,它们可能很有用。
COLLATE NOCASE 也会影响区分大小写的查询。
如果您有一列包含以下值:“aa”、“aA”
如果您使用 COLLATE NOCASE 创建列,它将返回“aa”和“aA”。 否则,如果您没有指定它,它将仅返回“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'
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)
排序规则不仅影响索引或结果的排序,还可能影响表上某些操作的有效性。 因此,您可能会发现将不区分大小写的文本列定义为主键(或使用
UNIQUE
约束)是合适的,如下所示:插入
id
值' 后a'
,无法添加另一个值为
'A'
的id
(仅因大小写而异)。 以下 SQL将失败并显示
尽管如此,仍然可以将
id
值从小写更改为大写:...这非常有趣,因为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:After inserting an
id
value'a'
,it's not possible to add another
id
with value'A'
(differing just by case). The following SQLwill fail with
It's nevertheless possible to change the
id
value from lowercase to uppercase:… which is quite interesting because the where clause actually works case-insensitively and the change is made from something
A
-like to a properA
.