|
SQL标准规范中规定了大量的关键词,这些关键词是不能用于表、索引、列、数据库、自定义函数、排序器、虚表以及其它命名对象的名字的。这个关键词列表非常长,以至于很少有人能全部记住。在多数SQL代码中,最安全的方式就是在自定义对象名字中不要使用任何英语单词。
The SQL standard specifies a huge number of keywords which may not
be used as the names of tables, indices, columns, databases, user-defined
functions, collations, virtual table modules, or any other named object.
The list of keywords is so long that few people can remember them all.
For most SQL code, your safest bet is to never use any English language
word as the name of a user-defined object.
如果你想在名字中使用关键词,需要使用引用方式。在SQLite中有四种引用关键词的方式:
If you want to use a keyword as a name, you need to quote it. There
are four ways of quoting keywords in SQLite:
'keyword' 用单引号引用表示这一个字符串。
A keyword in single quotes is a string literal."keyword" 用双引号引用表示这是一个标示符。
A keyword in double-quotes is an identifier.[keyword] 用方括号引用表示这是一个标示符。这不是SQL标准规范,这种引用方式使用于MS Access和SQL Server,SQLite为了兼容性支持这种方式。
A keyword enclosed in square brackets is an identifier. This is not standard SQL. This quoting mechanism is used by MS Access and SQL Server and is included in SQLite for compatibility.`keyword` 用重音符 (ASCII 码 96)引用表示这是一个标示符。这不是SQL标准规范,这种引用方式使用于MySQL,SQLite为了兼容性支持这种方式。
A keyword enclosed in grave accents (ASCII code 96) is an identifier. This is not standard SQL. This quoting mechanism is used by MySQL and is included in SQLite for compatibility.
为了保证面对历史SQL语句的适应性,SQLite通常倾向于下面的引用规则:
For resilience when confronted with historical SQL statements, SQLite
will sometimes bend the quoting rules above:
如果单引号引用的关键词(例如:'key' 或 'glob')用在了只接受标示符而不接受字符串的上下文环境时,会被当做一个标示符,而不是字符串。
If a keyword in single
quotes (ex: 'key' or 'glob') is used in a context where
an identifier is allowed but where a string literal is not allowed, then
the token is understood to be an identifier instead of a string literal.
如果双引号引用的关键词(例如:"key" 或 "glob")用在了只接受字符串而不接受标示符的上下文环境时,会被当做一个字符串,而不是标示符。
If a keyword in double
quotes (ex: "key" or "glob") is used in a context where
it cannot be resolved to an identifier but where a string literal
is allowed, then the token is understood to be a string literal instead
of an identifier.
程序员们应当尽量避免使用上面介绍的两个特例。提及这两个特例仅仅是为了能正确的运行过时的和不规范的SQL语句。未来的SQLite版本可能会在执行包含上面特例在内的一些畸形的语句时抛出错误,而不再执行。
Programmers are cautioned not to use the two exceptions described in
the previous bullets. We emphasize that they exist only so that old
and ill-formed SQL statements will run correctly. Future versions of
SQLite might raise errors instead of accepting the malformed
statements covered by the exceptions above.
SQLite会时不时的因增加新特性而添加新的关键词,所以为了保证你的代码不会被未来的增强特性破坏,你应当习惯性的对英语单词构成的标示符使用引用方式,即使目前不是必须的。
SQLite adds new keywords from time to time when it takes on new features.
So to prevent your code from being broken by future enhancements, you should
normally quote any identifier that is an English language word, even if
you do not have to.
下面的列表罗列了SQLite在各种编译期参数构建下的所有可能的关键词。大多数的配置都会用到下面多数甚至全部的关键词,不过当一些SQL语言特性被禁用时一些关键词会被忽略。在SQLite中,无论什么编译选项下,标示符只要不是下面罗列的124个词之一,就不会被SQL解析器当做关键词。
The list below shows all possible keywords used by any build of
SQLite regardless of compile-time options.
Most reasonable configurations use most or all of these keywords,
but some keywords may be omitted when SQL language features are
disabled.
Regardless of the compile-time configuration, any identifier that is not on
the following 124 element
list is not a keyword to the SQL parser in SQLite:
ABORT
ACTION
ADD
AFTER
ALL
ALTER
ANALYZE
AND
AS
ASC
ATTACH
AUTOINCREMENT
BEFORE
BEGIN
BETWEEN
BY
CASCADE
CASE
CAST
CHECK
COLLATE
COLUMN
COMMIT
CONFLICT
CONSTRAINT
CREATE
CROSS
CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP
DATABASE
DEFAULT
DEFERRABLE
DEFERRED
DELETE
DESC
DETACH
DISTINCT
DROP
EACH
ELSE
END
ESCAPE
EXCEPT
EXCLUSIVE
EXISTS
EXPLAIN
FAIL
FOR
FOREIGN
FROM
FULL
GLOB
GROUP
HAVING
IF
IGNORE
IMMEDIATE
IN
INDEX
INDEXED
INITIALLY
INNER
INSERT
INSTEAD
INTERSECT
INTO
IS
ISNULL
JOIN
KEY
LEFT
LIKE
LIMIT
MATCH
NATURAL
NO
NOT
NOTNULL
NULL
OF
OFFSET
ON
OR
ORDER
OUTER
PLAN
PRAGMA
PRIMARY
QUERY
RAISE
RECURSIVE
REFERENCES
REGEXP
REINDEX
RELEASE
RENAME
REPLACE
RESTRICT
RIGHT
ROLLBACK
ROW
SAVEPOINT
SELECT
SET
TABLE
TEMP
TEMPORARY
THEN
TO
TRANSACTION
TRIGGER
UNION
UNIQUE
UPDATE
USING
VACUUM
VALUES
VIEW
VIRTUAL
WHEN
WHERE
WITH
WITHOUT