定義
惯例
为了帮助指定 CQL 语法,我们将在本文档中使用以下惯例
-
语言规则将以非正式的 BNF 变体 表示法给出。具体来说,我们将使用方括号 (
[ item ]
) 表示可选项目,使用*
和+
表示重复项目(其中+
表示至少一个)。 -
为方便起见,语法还将使用以下惯例:非终结符术语将是小写(并链接到其定义),而终结符关键字将以“全部大写”提供。但请注意,关键字是
identifiers
,因此在实践中不区分大小写。我们还将使用 regexp 定义一些早期构造,我们将在re(<some regular expression>)
中指明。 -
语法是为了文档目的而提供的,并省略了一些小细节。例如,
CREATE TABLE
语句中最后一个列定义上的逗号是可选的,但如果存在,则受支持,即使本文档中的语法另有说明。此外,语法接受的所有内容不一定都是有效的 CQL。 -
正文中对关键字或 CQL 代码片段的引用将显示为
fixed-width font
。
标识符和关键字
CQL 语言使用标识符(或名称)来标识表、列和其他对象。标识符是与正则表达式 [a-zA-Z][a-zA-Z0-9_]*
匹配的令牌。
许多此类标识符(如 SELECT
或 WITH
)是关键字。它们对语言有固定的含义,并且大多数都是保留的。这些关键字的列表可以在 附录 A 中找到。
标识符和(未加引号的)关键字不区分大小写。因此,SELECT
与 select
或 sElEcT
相同,myId
与 myid
或 MYID
相同。经常使用的一种惯例(特别是本文档的示例)是对关键字使用大写,对其他标识符使用小写。
第二種識別碼稱為引號識別碼,定義為將任意字元序列(非空白)括在雙引號 ("
) 中。引號識別碼絕非關鍵字。因此,"select"
不是保留關鍵字,可用於參照欄位(請注意,特別不建議使用此方法),而 select
會引發剖析錯誤。此外,與未加引號的識別碼和關鍵字不同,引號識別碼區分大小寫("My Quoted Id"
與 "my quoted id"
不同)。不過,與 [a-zA-Z][a-zA-Z0-9_]*
相符的全小寫引號識別碼等同於移除雙引號所取得的未加引號識別碼(因此 "myid"
等同於 myid
和 myId
,但與 "myId"
不同)。在引號識別碼中,可以重複雙引號字元來跳脫它,因此 "foo "" bar"
是有效的識別碼。
引號識別碼可以宣告具有任意名稱的欄位,而這些名稱有時會與伺服器使用的特定名稱衝突。例如,使用條件更新時,伺服器會以包含名為 |
更正式地說,我們有
identifier::= unquoted_identifier | quoted_identifier
unquoted_identifier::= re('[a-zA-Z][link:[a-zA-Z0-9]]*')
quoted_identifier::= '"' (any character where " can appear if doubled)+ '"'
常數
CQL 定義下列常數
constant::= string | integer | float | boolean | uuid | blob | NULL
string::= ''' (any character where ' can appear if doubled)+ ''' : '$$' (any character other than '$$') '$$'
integer::= re('-?[0-9]+')
float::= re('-?[0-9]+(.[0-9]*)?([eE][+-]?[0-9+])?') | NAN | INFINITY
boolean::= TRUE | FALSE
uuid::= hex\{8}-hex\{4}-hex\{4}-hex\{4}-hex\{12}
hex::= re("[0-9a-fA-F]")
blob::= '0' ('x' | 'X') hex+
換句話說
-
字串常數是括在單引號 (
'
) 中的任意字元序列。可以透過重複單引號來包含單引號,例如'It''s raining today'
。這些不能與使用雙引號的引號識別碼混淆。或者,字串可以透過將任意字元序列括在兩個美元符號中來定義,在這種情況下,單引號可以使用而不需跳脫(It's raining today
)。後一種形式通常用於定義使用者定義函數,以避免在函數主體中跳脫單引號字元(因為它們比$$
更可能出現)。 -
整數、浮點數和布林常數的定義符合預期。不過,請注意浮點數允許特殊
NaN
和Infinity
常數。 -
CQL 支援UUID常數。
-
blob 的內容以十六進位提供,並以
0x
為字首。 -
特殊的
NULL
常數表示沒有值。
有關這些常數的類型,請參閱 資料類型 區段。
術語
CQL 有個概念稱為術語,表示 CQL 支援的值的種類。術語由下列定義
term::= constant | literal | function_call | arithmetic_operation | type_hint | bind_marker
literal::= collection_literal | vector_literal | udt_literal | tuple_literal
function_call::= identifier '(' [ term (',' term)* ] ')'
arithmetic_operation::= '-' term | term ('+' | '-' | '*' | '/' | '%') term
type_hint::= '(' cql_type ')' term
bind_marker::= '?' | ':' identifier
因此術語之一為
註解
CQL 中的註解是從雙破折號 (--
) 或雙斜線 (//
) 開始的行。
多行註解也支援透過包含在 /
和 /
中 (但不支援巢狀)。
-- This is a comment
// This is a comment too
/* This is
a multi-line comment */
陳述式
CQL 包含可分為下列類別的陳述式
-
data-definition
陳述式,用於定義和變更資料儲存方式 (鍵空間和表格)。 -
data-manipulation
陳述式,用於選取、插入和刪除資料。 -
secondary-indexes
陳述式。 -
materialized-views
陳述式。 -
cql-roles
陳述式。 -
cql-permissions
陳述式。 -
使用者定義函數 (UDF)
陳述式。 -
udts
陳述式。 -
cql-triggers
陳述式。
所有陳述都列在下方,並在本文檔的其餘部分中描述(請參閱上方連結)
cql_statement::= statement [ ';' ]
statement:=: ddl_statement :
| dml_statement
| secondary_index_statement
| materialized_view_statement
| role_or_permission_statement
| udf_statement
| udt_statement
| trigger_statement
ddl_statement::= use_statement
| create_keyspace_statement
| alter_keyspace_statement
| drop_keyspace_statement
| create_table_statement
| alter_table_statement
| drop_table_statement
| truncate_statement
dml_statement::= select_statement
| insert_statement
| update_statement
| delete_statement
| batch_statement
secondary_index_statement::= create_index_statement
| drop_index_statement
materialized_view_statement::= create_materialized_view_statement
| drop_materialized_view_statement
role_or_permission_statement::= create_role_statement
| alter_role_statement
| drop_role_statement
| grant_role_statement
| revoke_role_statement
| list_roles_statement
| grant_permission_statement
| revoke_permission_statement
| list_permissions_statement
| create_user_statement
| alter_user_statement
| drop_user_statement
| list_users_statement
udf_statement::= create_function_statement
| drop_function_statement
| create_aggregate_statement
| drop_aggregate_statement
udt_statement::= create_type_statement
| alter_type_statement
| drop_type_statement
trigger_statement::= create_trigger_statement
| drop_trigger_statement