Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are quotes around tables and columns in a MySQL query really necessary?

Tags:

php

mysql

I have a short question about mysql query.

What is correct?

SELECT * FROM Persons WHERE Year='1965'

Or

SELECT * FROM `Persons` WHERE `Year` = '1965'

Is this a personal choice or is this something what is really wrong?

like image 483
JochemQuery Avatar asked Apr 26 '12 17:04

JochemQuery


People also ask

When should I use quotes in MySQL?

single quotes are used to indicate the beginning and end of a string in SQL. Double quotes generally aren't used in SQL, you use them in order to store lets say the whole SQL statement(query), in a variable so you can use it later in your code.

Do column names need quotes in SQL?

If the table names and the column names are shown in lowercase letters this means they are delimited and quotation marks need to be used for the table names and the column names when executing SQL queries against the Oracle database.

Does MySQL use single or double quotes?

Double quotes are supported by MySQL for string values as well, but single quotes are more widely accepted by other RDBMS, so it is a good habit to use single quotes instead of double.

Why does MySQL use Backticks?

Backticks are used in MySQL to select columns and tables from your MySQL source. In the example below we are calling to the table titled Album and the column Title . Using backticks we are signifying that those are the column and table names.


2 Answers

Quotes are needed if your identifiers (column, table names, operators, etc.) contain MySQL reserved words.

See here for the complete list of reserved words: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

like image 62
Cody Caughlan Avatar answered Sep 18 '22 17:09

Cody Caughlan


Both are correct, but the second one will ALWAYS be accepted, even when you use keywords or functions like while and NOW() that would normally be seen as operators.

like image 45
Manuel Avatar answered Sep 17 '22 17:09

Manuel