gpt4 book ai didi

sql - 在 PostgreSQL 中创建一定长度的索引时用双括号

转载 作者:行者123 更新时间:2023-11-29 13:15:22 25 4
gpt4 key购买 nike

要创建具有特定长度的索引,我使用以下查询:

CREATE INDEX users_full_name_idx ON users ((full_name::varchar(16)));

这段代码工作正常。但是,如果我尝试执行以下操作:

CREATE INDEX users_full_name_idx ON users (full_name::varchar(16));

我收到错误。谁能解释为什么我们需要两个而不是一个牙套。

最佳答案

index on expression :

An index column need not be just a column of the underlying table, but can be a function or scalar expression computed from one or more columns of the table. This feature is useful to obtain fast access to tables based on the results of computations.

CREATE INDEX test1_lower_col1_idx ON test1 (lower(col1));

CREATE INDEX people_names ON people ((first_name || ' ' || last_name));

The syntax of the CREATE INDEX command normally requires writing parentheses around index expressions, as shown in the second example. The parentheses can be omitted when the expression is just a function call, as in the first example.

CREATE INDEX :

The key field(s) for the index are specified as column names, or alternatively as expressions written in parentheses.

关于sql - 在 PostgreSQL 中创建一定长度的索引时用双括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49925791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com