Are quotes important in mysql queries?

4

Specifically, I mean these - > '' when selecting the fields to consult.

I understand that when entering values if it is important to tell if it is a number or a string, but to select the fields works whether you put them or not.

That is why I am not sure if they are important or not.

    
asked by Killpe 08.01.2018 в 12:29
source

1 answer

9

First it is important to distinguish between these two types of quotes:

  • ' ' : which are called in MySQL identification quotes (in English they are called backticks )
  • ' ' : these are the single quotes of a lifetime.

& rtrif; The identification marks ' '

They are only mandatory when in the query you are using a table or column name that is a MySQL keyword . In that case, if you do not use them, the query would be wrong because it will confuse your table or column with that reserved word.

For example, if in a table you have a column called sum , given that SUM is a reserved MySQL word, this query would be wrong:

SELECT sum FROM tu_tabla;

To avoid the error, you will have to surround the column with identification quotes:

SELECT 'sum' FROM tu_tabla;

So the query will work.

Note: I would recommend not using reserved words in table or column names.

& rtrif; The single quotes ' '

These are the ones used to indicate that it is a string and not a numeric or Boolean value. They are not the same as the identification quotes .

This query will work without problems:

SELECT 'sum' FROM tu_tabla WHERE nombre='Pedro';

But if you use identification marks for Pedro the handler will interpret that Pedro is a column. So when executing this query:

SELECT 'sum' FROM tu_tabla WHERE nombre='Pedro';

It will throw you the following error:

  

Unknown column 'Pedro' in 'where clause'

    
answered by 08.01.2018 / 13:00
source