热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

关于backtick(')的用法,SQL标准说明了什么?-WhatdoestheSQLStandardsayaboutusageofbacktick(`)?

OnceIhadspenthoursindebuggingasimpleSQLqueryusingmysql_query()inPHPMySQLonlytoreal

Once I had spent hours in debugging a simple SQL query using mysql_query() in PHP/MySQL only to realise that I had missed bactick around the table name. From then I had been always using it around table names.

有一次,我花了几个小时在PHP/MySQL中使用mysql_query()调试一个简单的SQL查询,结果发现我漏掉了表名中的bactick。从那时起,我就一直用它来称呼桌子的名字。

But when I used the same in SQLite/C++, the symbol is not even recognized. It's confusing, whether to use this or not? What does standard say about usage of it?

但是,当我在SQLite/ c++中使用相同的符号时,这个符号甚至没有被识别。用不用这个很让人困惑?标准对它的使用有什么规定?

Also, it would be helpful if anyone could tell me when to use quotes and when not. I mean around values and field names.

另外,如果有人能告诉我什么时候该用引号,什么时候不该用引号,那将会很有帮助。我的意思是关于值和字段名。

1 个解决方案

#1


69  

The SQL standard (current version is ISO/IEC 9075:2011, in multiple parts) says nothing about the 'back-tick' or 'back-quote' symbol (Unicode U+0060 or GRAVE ACCENT); it doesn't recognize it as a character with special meaning that can appear in SQL.

SQL标准(目前的版本是ISO/IEC 9075:2011,有很多部分)没有提到“后勾”或“后引号”符号(Unicode U+0060或庄重的重音);它不承认它是一个具有特殊意义的字符,可以出现在SQL中。

The Standard SQL mechanism for quoting identifiers is with delimited identifiers enclosed in double quotes:

引用标识符的标准SQL机制是用带分隔符的双引号括起来的:

SELECT "select" FROM "from" WHERE "where" = "group by";

In MySQL, that might be written:

在MySQL中,可以这样写:

SELECT `select` FROM `from` WHERE `where` = `group by`;

In MS SQL Server, that might be written:

在MS SQL Server中,可以这样写:

SELECT [select] FROM [from] WHERE [where] = [group by];

The trouble with the SQL Standard notation is that C programmers are used to enclosing strings in double quotes, so most DBMS use double quotes as an alternative to the single quotes recognized by the standard. But that then leaves you with a problem when you want to enclose identifiers.

SQL标准表示法的问题在于,C程序员习惯于将字符串括在双引号中,因此大多数DBMS都使用双引号作为标准识别的单引号的替代。但当您想要包含标识符时,这会给您带来一个问题。

Microsoft took one approach; MySQL took another; Informix allows interchangeable use of single and double quotes, but if you want delimited identifiers, you set an environment variable and then you have to follow the standard (single quotes for strings, double quotes for identifiers); DB2 only follows the standard, AFAIK; SQLite appears to follow the standard; Oracle also appears to follow the standard; Sybase appears to allow either double quotes (standard) or square brackets (as with MS SQL Server — which means SQL Server might allow double quotes too). This page documents all these servers (and was helpful filling out the gaps in my knowledge), and notes whether the strings inside delimited identifiers are case-sensitive or not.

微软的一种方法;MySQL又;Informix允许使用单引号和双引号,但如果您想要分隔标识符,您需要设置一个环境变量,然后必须遵循标准(字符串的单引号、标识符的双引号);DB2只遵循标准AFAIK;SQLite似乎遵循标准;甲骨文似乎也遵循这一标准;Sybase似乎允许双引号(标准)或方括号(就像MS SQL Server一样——这意味着SQL Server也可能允许双引号)。这个页面记录了所有这些服务器(这有助于填补我所知道的空白),并注意分隔符内的字符串是否区分大小写。


As to when to use a quoting mechanism around identifiers, my attitude is 'never'. Well, not quite never, but only when absolutely forced into doing so.

至于何时在标识符周围使用引用机制,我的态度是“从不”。好吧,不是完全没有,但只有在绝对被迫这么做的时候。

Note that delimited identifiers are case-sensitive; that is, "from" and "FROM" refer to different columns (in most DBMS — see URL above). Most of SQL is not case-sensitive; it is a nuisance to know which case to use. (The SQL Standard has a mainframe orientation — it expects names to be converted to upper-case; most DBMS convert names to lower-case, though.)

注意分隔标识符是区分大小写的;也就是说,“from”和“from”指的是不同的列(在大多数DBMS中——见上面的URL)。大多数SQL不是大小写敏感的;知道使用哪种情况是令人讨厌的。(SQL标准有大型机方向——它希望将名称转换为大写;不过,大多数DBMS都会将名称转换为小写。

In general, you must delimit identifiers which are keywords to the version of SQL you are using. That means most of the keywords in Standard SQL, plus any extras that are part of the particular implementation(s) that you are using.

通常,您必须将标识符分隔开,这些标识符是您正在使用的SQL版本的关键字。这意味着标准SQL中的大多数关键字,以及您正在使用的特定实现的一部分。

One continuing source of trouble is upgrades, where a column name that was not a keyword in release N becomes a keyword in release N+1. Existing SQL that worked before the upgrade stops working afterwards. Then, at least as a short-term measure, you may be forced into quoting the name. But in the ordinary course of events, you should aim to avoid needing to quote identifiers.

一个持续的麻烦来源是升级,其中不是关键字的列名在N+1版本中成为关键字。在升级之前工作的现有SQL之后将停止工作。然后,至少作为短期措施,你可能会被迫引用这个名字。但是在一般情况下,您应该避免引用标识符。

Of course, my attitude is coloured by the fact that Informix (which is what I work with mostly) accepts this SQL verbatim, whereas most DBMS would choke on it:

当然,我的态度受到Informix(我主要使用Informix)逐字接受这个SQL的影响,而大多数DBMS都会对它产生瓶颈:

CREATE TABLE TABLE
(
    DATE    INTEGER NOT NULL,
    NULL    FLOAT   NOT NULL,
    FLOAT   INTEGER NOT NULL,
    NOT     DATE    NOT NULL,
    INTEGER FLOAT   NOT NULL
);

Of course, the person who produces such a ridiculous table for anything other than demonstration purposes should be hung, drawn, quartered and then the residue should be made to fix the mess they've created. But, within some limits which customers routinely manage to hit, keywords can be used as identifiers in many contexts. That is, of itself, a useful form of future-proofing. If a word becomes a keyword, there's a moderate chance that the existing code will continue to work unaffected by the change. However, the mechanism is not perfect; you can't create a table with a column called PRIMARY, but you can alter a table to add such a column. There is a reason for the idiosyncrasy, but it is hard to explain.

当然,除了为了演示目的而制作如此可笑的桌子的人应该被挂起来,画出来,分成四等分,然后剩下的部分应该被用来修复他们造成的混乱。但是,在一些客户经常遇到的限制范围内,关键字可以在许多上下文中用作标识符。这本身就是一种有用的未来防护形式。如果一个单词变成了关键字,那么现有的代码将继续运行,而不受更改的影响。然而,这种机制并不完善;不能使用名为PRIMARY的列创建表,但可以修改表以添加此类列。这种癖好是有原因的,但很难解释。


推荐阅读
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • mysqldinitializeconsole失败_mysql03误删除了所有用户解决办法
    误删除了所有用户解决办法第一种方法(企业常用)1.将数据库down掉[rootdb03mysql]#etcinit.dmysqldstopShuttingdownMySQL..SU ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 深入理解Kafka服务端请求队列中请求的处理
    本文深入分析了Kafka服务端请求队列中请求的处理过程,详细介绍了请求的封装和放入请求队列的过程,以及处理请求的线程池的创建和容量设置。通过场景分析、图示说明和源码分析,帮助读者更好地理解Kafka服务端的工作原理。 ... [详细]
  • 本文介绍了一个React Native新手在尝试将数据发布到服务器时遇到的问题,以及他的React Native代码和服务器端代码。他使用fetch方法将数据发送到服务器,但无法在服务器端读取/获取发布的数据。 ... [详细]
  • 恶意软件分析的最佳编程语言及其应用
    本文介绍了学习恶意软件分析和逆向工程领域时最适合的编程语言,并重点讨论了Python的优点。Python是一种解释型、多用途的语言,具有可读性高、可快速开发、易于学习的特点。作者分享了在本地恶意软件分析中使用Python的经验,包括快速复制恶意软件组件以更好地理解其工作。此外,作者还提到了Python的跨平台优势,使得在不同操作系统上运行代码变得更加方便。 ... [详细]
  • PeopleSoft安装镜像版本及导入语言包的方法
    本文介绍了PeopleSoft安装镜像的两个版本,分别是VirtualBox虚拟机版本和NativeOS版本,并详细说明了导入语言包的方法。对于Windows版本,可以通过psdmt.exe登录进入,并使用datamover脚本导入语言包。对于Linux版本,同样可以使用命令行方式执行datamover脚本导入语言包。导入语言包后,可以实现多种语言的登录。参考文献提供了相关链接以供深入了解。 ... [详细]
  • Question该提问来源于开源项目:react-native-device-info/react-native-device-info ... [详细]
  • 本博文基于《Amalgamationofproteinsequence,structureandtextualinformationforimprovingprote ... [详细]
  • [转载]从零开始学习OpenGL ES之四 – 光效
    继续我们的iPhoneOpenGLES之旅,我们将讨论光效。目前,我们没有加入任何光效。幸运的是,OpenGL在没有设置光效的情况下仍然可 ... [详细]
author-avatar
我爱麦兜李
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有