229

Is SQL case sensitive? I've used MySQL and SQL Server which both seem to be case insensitive. Is this always the case? Does the standard define case-sensitivity?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steve T
  • 7,729
  • 6
  • 45
  • 65

11 Answers11

215

The SQL keywords are case insensitive (SELECT, FROM, WHERE, etc), but they are often written in all caps. However, in some setups, table and column names are case sensitive.

MySQL has a configuration option to enable/disable it. Usually case sensitive table and column names are the default on Linux MySQL and case insensitive used to be the default on Windows, but now the installer asked about this during setup. For SQL Server it is a function of the database's collation setting.

Here is the MySQL page about name case-sensitivity

Here is the article in MSDN about collations for SQL Server

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stefan Rusek
  • 4,737
  • 2
  • 28
  • 25
  • 10
    Some systems (like PostgreSQL) are case sensitive in table and column names, but attempt to hide it through lowercasing or uppercasing all names before looking them up. On these systems you will have to enclose the table name in "double quotes" to make sure the exact name you entered is looked up. – Michael Ratanapintha Sep 30 '08 at 17:12
  • 2
    "but are often written in all caps" I disagree, that's merely preference, I have always seen the opposite actually – BlackTigerX Feb 06 '09 at 17:53
  • 4
    For instance if MS Sql server is installed using case sensitive collation, then table, column, variable names become case sensitive, even if database has case insensitive collation. – Vadym Stetsiak Sep 17 '09 at 08:34
  • 4
    @BlackTigerX - The Oracle manuals have all example SQL with keywords (SELECT, FROM, WHERE, etc) written in uppercase but table and column-names in lower case. – J. Polfer Jun 24 '10 at 13:53
  • Hmmm, is this still true of mysql? I thought that I have a default install of mysql, and it is case insensitive for column names. – Kzqai May 26 '11 at 16:46
  • actually according to the mysql manual (http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html) The column names are case insensitive – Ittai Dec 07 '11 at 07:07
  • 1
    I have recently (about a week ago) installed the latest MySQL 5.7.21 server on Windows. Its table and column names are case-insensitive but this settings was not asked by the installer during setup. – Snackoverflow Feb 28 '18 at 04:13
  • For MySQL each character column can have its collation configured independently, affecting whether queries against that column are case-sensitive or not. (Details [here](https://dev.mysql.com/doc/refman/8.0/en/charset-column.html).) – yoyo May 18 '21 at 23:36
25

This isn't strictly SQL language, but in SQL Server if your database collation is case-sensitive, then all table names are case-sensitive.

Cade Roux
  • 88,164
  • 40
  • 182
  • 265
24

The SQL-92 specification states that identifiers might be quoted, or unquoted. If both sides are unquoted then they are always case insensitive, e.g., table_name == TAble_nAmE.

However, quoted identifiers are case sensitive, e.g., "table_name" != "TAble_naME". Also based on the specification if you wish to compare unquoted identifiers with quoted ones, then unquoted and quoted identifiers can be considered the same, if the unquoted characters are uppercased, e.g. TABLE_NAME == "TABLE_NAME", but TABLE_NAME != "table_name" or TABLE_NAME != "TAble_NaMe".

Here is the relevant part of the specification (section 5.2.13):

  1. 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>".

Note, that just like with other parts of the SQL standard, not all databases follow this section fully. PostgreSQL for example stores all unquoted identifiers lowercased instead of uppercased, so table_name == "table_name" (which is exactly the opposite of the standard). Also some databases are case insensitive all the time, or case-sensitiveness depend on some setting in the DB or are dependent on some of the properties of the system, usually whether the file system is case sensitive or not.

Note that some database tools might send identifiers quoted all the time, so in instances where you mix queries generated by some tool (like a CREATE TABLE query generated by Liquibase or other DB migration tool), with hand made queries (like a simple JDBC select in your application) you have to make sure that the cases are consistent, especially on databases where quoted and unquoted identifiers are different (DB2, PostgreSQL, etc.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SztupY
  • 10,291
  • 8
  • 64
  • 87
18

In SQL Server it is an option. Turning it on sucks.

I'm not sure about MySQL.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JosephStyons
  • 57,317
  • 63
  • 160
  • 234
  • In MySql, case-insensitivity is an option you can turn on and off. Just that insensitivity doesn't work as you'd assume it would do on Linux if the file-system is case-sensitive (default). You have to make a case-insensitive file-system on Linux in order for mysql case-insensitivity to work the same way as on windows (=properly). Especially turning it on/off after some work in one another mode can bear bad consequences. – Stefan Steiger Jun 06 '14 at 07:23
14

Identifiers and reserved words should not be case sensitive, although many follow a convention to use capitals for reserved words and upper camel case for identifiers.

See SQL-92 Sec. 5.2

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Turnkey
  • 9,266
  • 3
  • 27
  • 36
13

My understanding is that the SQL standard calls for case-insensitivity. I don't believe any databases follow the standard completely, though.

MySQL has a configuration setting as part of its "strict mode" (a grab bag of several settings that make MySQL more standards-compliant) for case sensitive or insensitive table names. Regardless of this setting, column names are still case-insensitive, although I think it affects how the column-names are displayed. I believe this setting is instance-wide, across all databases within the RDBMS instance, although I'm researching today to confirm this (and hoping the answer is no).

I like how Oracle handles this far better. In straight SQL, identifiers like table and column names are case insensitive. However, if for some reason you really desire to get explicit casing, you can enclose the identifier in double-quotes (which are quite different in Oracle SQL from the single-quotes used to enclose string data). So:

SELECT fieldName
FROM tableName;

will query fieldname from tablename, but

SELECT "fieldName"
FROM "tableName";

will query fieldName from tableName.

I'm pretty sure you could even use this mechanism to insert spaces or other non-standard characters into an identifier.

In this situation if for some reason you found explicitly-cased table and column names desirable it was available to you, but it was still something I would highly caution against.

My convention when I used Oracle on a daily basis was that in code I would put all Oracle SQL keywords in uppercase and all identifiers in lowercase. In documentation I would put all table and column names in uppercase. It was very convenient and readable to be able to do this (although sometimes a pain to type so many capitals in code -- I'm sure I could've found an editor feature to help, here).

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.

Here's another Stack Overflow question that gets into discussing if case-sensitivity is desirable in an RDBMS.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
skiphoppy
  • 97,646
  • 72
  • 174
  • 218
5

No. MySQL is not case sensitive, and neither is the SQL standard. It's just common practice to write the commands upper-case.

Now, if you are talking about table/column names, then yes they are, but not the commands themselves.

So

SELECT * FROM foo;

is the same as

select * from foo;

but not the same as

select * from FOO;
cmcculloh
  • 47,596
  • 40
  • 105
  • 130
  • 3
    In most RDBMS, table names aren't case sensitive either. At least not by default. MySQL is the most prominent exception to this rule. –  Jul 12 '13 at 08:35
4

I found this blog post to be very helpful (I am not the author). Summarizing (please read, though):

...delimited identifiers are case sensitive ("table_name" != "Table_Name"), while non quoted identifiers are not, and are transformed to upper case (table_name => TABLE_NAME).

He found DB2, Oracle and Interbase/Firebird are 100% compliant:

PostgreSQL ... lowercases every unquoted identifier, instead of uppercasing it. MySQL ... file system dependent. SQLite and SQL Server ... case of the table and field names are preserved on creation, but they are completely ignored afterwards.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
Matthew Cornell
  • 4,114
  • 3
  • 27
  • 40
3

I don't think SQL Server is case sensitive, at least not by default.

When I'm querying manually via SQL Server Management Studio, I mess up case all the time and it cheerfully accepts it:

select cOL1, col2 FrOM taBLeName WheRE ...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dana
  • 32,083
  • 17
  • 62
  • 73
2

SQL keywords are case insensitive themselves.

Names of tables, columns, etc., have a case sensitivity which is database dependent - you should probably assume that they are case sensitive unless you know otherwise (in many databases they aren't though; in MySQL table names are sometimes case sensitive, but most other names are not).

Comparing data using =, >, <, etc., has a case awareness which is dependent on the collation settings which are in use on the individual database, table or even column in question. It's normal however, to keep collation fairly consistent within a database. We have a few columns which need to store case sensitive values; they have a collation specifically set.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MarkR
  • 62,604
  • 14
  • 116
  • 151
0

Have the best of both worlds

These days you can just write all your SQL statements in lowercase and if you ever need to have it formatted then just install a plugin that will do it for you. This is only applicable if your code editor has those plug-ins available. Visual Studio Code has many extensions that can do this.

Here's a couple you can use: vscode-sql-formatter and SqlFormatter-VSCode

Isaac Pak
  • 4,467
  • 3
  • 42
  • 48