Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lowercase or uppercase for SQL Statements

Tags:

syntax

sql

Is there a fundamental difference between these statements

"select * from users where id = 1"
OR
"SELECT* FROM users WHERE id = 1"
OR
'select * from users where id = 1'
OR
'SELECT * FROM users WHERE id = 1'

I was just wondering... I always used the 2nd method, but some collegues are bound to use other methods. What is the most common convention?

like image 618
Michiel Avatar asked Sep 27 '11 08:09

Michiel


People also ask

Can I use lowercase in SQL?

Whenever you want some text data from your SQL database to be displayed in lowercase, use the LOWER() function. This function takes as an argument a string or the name of a column whose text values are to be displayed in lowercase.

Is SQL capital sensitive?

The SQL keywords (SELECT, FROM, WHERE, etc.) are case-insensitive, yet they are frequently expressed in all capitals. Table and column names are case-sensitive in some settings. MySQL provides a setting that allows you to enable or disable it.

What is proper case in SQL?

The Proper Case processor converts text attribute values to upper case for the first character of each word and to lower case for subsequent characters in the word.

Does capitalization matter in SQL table names?

Table names are stored in lowercase on disk and name comparisons are not case-sensitive. MySQL converts all table names to lowercase on storage and lookup. This behavior also applies to database names and table aliases.


2 Answers

You have two separate issues:

  1. Whether you use uppercase or lowercase for SQL keywords. This is a matter of taste. Documentation usually uses uppercase, but I suggest you just agree on either in the particular project.

  2. How to quote SQL in your host language. Since strings are quoted with single quotes (') in SQL, I suggest you use double quotes (") to quote SQL statements from your host language (or tripple quotes (""") if your host language is python). SQL does use double quotes, but only for names of tables, columns and functions that contain special characters, which you can usually avoid needing.

    In C/C++ there is a third option for quoting. If your compiler supports variadic macros (introduced in C99), you can define:

    #define SQL(...) #__VA_ARGS__
    

    and use SQL like SQL(SELECT * FROM users WHERE id = 1). The advantage is that this way the string can span multiple lines, while quoted strings must have each line quoted separately. The tripple quotes in python serve the same purpose.

like image 107
Jan Hudec Avatar answered Oct 22 '22 21:10

Jan Hudec


There is no difference in terms of how it will work, but usually I put SQL keywords in uppercase so it would be:

SELECT * FROM users WHERE id = 1
like image 43
AndrewC Avatar answered Oct 22 '22 21:10

AndrewC