SQL区分大小写吗?我用过MySQL和SQL Server,它们似乎都是不区分大小写的。情况总是这样吗?标准是否定义了区分大小写?


当前回答

SQL-92规范规定标识符可以被引用,也可以不被引用。如果两边都没有引号,那么它们总是不区分大小写的,例如,table_name == table_name。

但是,带引号的标识符是区分大小写的,例如,"table_name" != "table_name"。同样根据规范,如果你想比较不带引号的标识符和带引号的标识符,那么不带引号的标识符和带引号的标识符可以被认为是相同的,如果不带引号的字符是大写的,例如TABLE_NAME == "TABLE_NAME",但是TABLE_NAME != "TABLE_NAME"或TABLE_NAME != "TABLE_NAME"。

以下是规范的相关部分(第5.2.13节):

A <regular identifier> and a <delimited identifier> are equivalent if the <identifier body> of the <regular identifier> (with every letter that is a lower-case letter replaced by the equivalent upper-case letter or letters) and the <delimited identifier body> of the <delimited identifier> (with all occurrences of <quote> replaced by <quote symbol> and all occurrences of <doublequote symbol> replaced by <double quote>), considered as the repetition of a <character string literal> that specifies a <character set specification> of SQL_TEXT and an implementation- defined collation that is sensitive to case, compare equally according to the comparison rules in Subclause 8.2, "<comparison predicate>".

注意,就像SQL标准的其他部分一样,并非所有数据库都完全遵循本节。例如,PostgreSQL存储的所有未加引号的标识符都是小写的,而不是大写的,因此table_name == "table_name"(这与标准完全相反)。另外,有些数据库始终是不区分大小写的,或者区分大小写取决于数据库中的某些设置,或者取决于系统的某些属性,通常是文件系统是否区分大小写。

注意,一些数据库工具可能会一直发送带引号的标识符,因此在混合使用某些工具生成的查询(如由Liquibase或其他DB迁移工具生成的CREATE TABLE查询)和手工查询(如应用程序中的简单JDBC选择)的情况下,必须确保情况一致,特别是在带引号和不带引号的标识符不同的数据库(DB2、PostgreSQL等)上。

其他回答

我的理解是,SQL标准要求不区分大小写。不过,我不相信任何数据库都完全遵循标准。

MySQL有一个配置设置,作为它的“严格模式”(使MySQL更符合标准的几个设置的集合)的一部分,用于区分大小写或不敏感的表名。不管这个设置如何,列名仍然是不区分大小写的,尽管我认为它会影响列名的显示方式。我相信这个设置是实例级的,横跨RDBMS实例中的所有数据库,尽管我今天正在研究以确认这一点(并希望答案是否定的)。

我更喜欢甲骨文的处理方式。在直接SQL中,表名和列名等标识符是不区分大小写的。然而,如果出于某种原因,您确实希望获得显式的大小写,您可以将标识符包含在双引号中(在Oracle SQL中,双引号与用于包含字符串数据的单引号有很大不同)。所以:

SELECT fieldName
FROM tableName;

将从表名查询字段名,但是

SELECT "fieldName"
FROM "tableName";

将从tableName中查询fieldName。

我非常确定您甚至可以使用这种机制在标识符中插入空格或其他非标准字符。

在这种情况下,如果出于某种原因,您发现显式大小写表名和列名是可取的,那么您可以使用它,但这仍然是我要高度警告的。

当我每天使用Oracle时,我的习惯是在代码中,我将所有Oracle SQL关键字都大写,所有标识符都小写。在文档中,我将所有表和列名都用大写。这样做是非常方便和易读的(尽管有时在代码中键入如此多的大写字母是一种痛苦——我相信我可以在这里找到一个编辑器功能来帮助)。

In my opinion MySQL is particularly bad for differing about this on different platforms. We need to be able to dump databases on Windows and load them into Unix, and doing so is a disaster if the installer on Windows forgot to put the RDBMS into case-sensitive mode. (To be fair, part of the reason this is a disaster is our coders made the bad decision, long ago, to rely on the case-sensitivity of MySQL on UNIX.) The people who wrote the Windows MySQL installer made it really convenient and Windows-like, and it was great to move toward giving people a checkbox to say "Would you like to turn on strict mode and make MySQL more standards-compliant?" But it is very convenient for MySQL to differ so significantly from the standard, and then make matters worse by turning around and differing from its own de facto standard on different platforms. I'm sure that on differing Linux distributions this may be further compounded, as packagers for different distros probably have at times incorporated their own preferred MySQL configuration settings.

下面是另一个Stack Overflow问题,讨论在RDBMS中是否需要区分大小写。

不。MySQL不区分大小写,SQL标准也不区分大小写。通常的做法是将命令写成大写。

现在,如果您谈论的是表/列名,那么是的,它们是,但不是命令本身。

So

SELECT * FROM foo;

select * from foo;

但不一样

select * from FOO;

两全其美

现在你可以用小写字母写所有的SQL语句,如果你需要格式化它,只要安装一个插件就可以了。这只适用于代码编辑器有这些插件的情况。Visual Studio Code有许多扩展可以做到这一点。

这里有两个你可以使用的:vcode -sql-formatter和SqlFormatter-VSCode

SQL-92规范规定标识符可以被引用,也可以不被引用。如果两边都没有引号,那么它们总是不区分大小写的,例如,table_name == table_name。

但是,带引号的标识符是区分大小写的,例如,"table_name" != "table_name"。同样根据规范,如果你想比较不带引号的标识符和带引号的标识符,那么不带引号的标识符和带引号的标识符可以被认为是相同的,如果不带引号的字符是大写的,例如TABLE_NAME == "TABLE_NAME",但是TABLE_NAME != "TABLE_NAME"或TABLE_NAME != "TABLE_NAME"。

以下是规范的相关部分(第5.2.13节):

A <regular identifier> and a <delimited identifier> are equivalent if the <identifier body> of the <regular identifier> (with every letter that is a lower-case letter replaced by the equivalent upper-case letter or letters) and the <delimited identifier body> of the <delimited identifier> (with all occurrences of <quote> replaced by <quote symbol> and all occurrences of <doublequote symbol> replaced by <double quote>), considered as the repetition of a <character string literal> that specifies a <character set specification> of SQL_TEXT and an implementation- defined collation that is sensitive to case, compare equally according to the comparison rules in Subclause 8.2, "<comparison predicate>".

注意,就像SQL标准的其他部分一样,并非所有数据库都完全遵循本节。例如,PostgreSQL存储的所有未加引号的标识符都是小写的,而不是大写的,因此table_name == "table_name"(这与标准完全相反)。另外,有些数据库始终是不区分大小写的,或者区分大小写取决于数据库中的某些设置,或者取决于系统的某些属性,通常是文件系统是否区分大小写。

注意,一些数据库工具可能会一直发送带引号的标识符,因此在混合使用某些工具生成的查询(如由Liquibase或其他DB迁移工具生成的CREATE TABLE查询)和手工查询(如应用程序中的简单JDBC选择)的情况下,必须确保情况一致,特别是在带引号和不带引号的标识符不同的数据库(DB2、PostgreSQL等)上。

我不认为SQL Server是区分大小写的,至少默认情况下不是。

当我通过SQL Server Management Studio手动查询时,我总是搞砸情况,它很高兴地接受它:

select cOL1, col2 FrOM taBLeName WheRE ...