【问题标题】:SQLite table and column name requirementsSQLite 表和列名要求
【发布时间】:2021-11-30 19:17:59
【问题描述】:

我想知道 SQLite 在创建表时对表名和列名施加了哪些限制。 creating a table 的文档说表名不能以“sqlite_”开头,但还有哪些其他限制?是否有任何有效的正式定义?

只要名称被引用,SQLite 似乎令人惊讶地接受。比如……

sqlite> create table 'name with spaces, punctuation & $pecial characters?'(x int);
sqlite> .tables
name with spaces, punctuation & $pecial characters?

【问题讨论】:

    标签: sqlite


    【解决方案1】:

    如果您使用括号或引号,您可以使用任何名称,并且没有限制:

    create table [--This is a_valid.table+name!?] (x int);
    

    但不带括号的表名应该是任何不以数字开头且不包含任何空格的字母数字组合。

    您可以使用下划线和$,但不能使用以下符号:+ - ? ! * @ % ^ & # = / \ : " '

    【讨论】:

    • 我最近浏览了 SQLite 关于标识符的文档,值得注意的是,为了与 MS Access 和 SQL Server 兼容,允许使用方括号,但双引号是标准的,并且看起来是等效的。 sqlite.org/lang_keywords.html
    【解决方案2】:

    来自sqlite doc

    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]       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`       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.
    

    所以,双引号表名,你可以使用任何字符。 [tablename] 可以使用,但不是标准 SQL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2010-10-30
      相关资源
      最近更新 更多