Why do space aliases only work with backticks?

Asked

Viewed 85 times

1

I have a view where all its columns have been dubbed to make it more readable:

+----------------+---------------+------+-----+---------+-------+
| Field          | Type          | Null | Key | Default | Extra |
+----------------+---------------+------+-----+---------+-------+
| CNPJ           | char(14)      | NO   |     | NULL    |       |
| DATA CADASTRO  | date          | YES  |     | NULL    |       |
| RAZÃO SOCIAL   | varchar(150)  | YES  |     | NULL    |       |
| OPERADORA      | varchar(100)  | YES  |     | NULL    |       |
| LINHAS         | int(11)       | YES  |     | NULL    |       |
| CLASSIFICAÇÃO  | varchar(2)    | NO   |     |         |       |
| VIGENCIA       | date          | YES  |     | NULL    |       |
| MESES CONTRATO | int(21)       | YES  |     | NULL    |       |
| FIDELIDADE     | varchar(16)   | NO   |     |         |       |
| VALOR GASTO    | decimal(10,2) | YES  |     | NULL    |       |
| FIXO EMPRESA   | varchar(17)   | YES  |     | NULL    |       |
| GESTOR         | varchar(150)  | YES  |     | NULL    |       |
+----------------+---------------+------+-----+---------+-------+

Note that there are some fields where there is space between a word and another as for example RAZÃO SOCIAL and if I try to execute such a query:

SELECT * FROM listarclientes WHERE "RAZÃO SOCIAL" like "EMPRESA FICTICIA LTDA ME";

It does not work for me that this company exists in my database. Now if I try to do the same but with the use of backticks:

SELECT * FROM listarclientes WHERE `RAZÃO SOCIAL` like "EMPRESA FICTICIA LTDA ME";

Now yes it brings me the desired result. Why do you consult with aliases which contains space in Mysql require the use of backticks?


NOTE: in the queries tested both with simple quotes ' as with double quotes " and both result in blank.

NOTE²: the statement of aliases in view in this example were made with double quotes " but I also tested with simple quotes ', and in the case without any kind of quotation marks (as is most common) it would not be possible in this case because it is composed of two words with space.

1 answer

1


Because space closes the identifier name. There are no space names. Identifiers are words, they must only have a name, so let’s say that there is a law that requires that you can only have a first and last name, nothing else, and you like the name João Alberto, to comply with this law you must give the name of your son Joãoalberto, so it is one thing. Most languages have this law because it becomes too complicated for the compiler to seek to resolve ambiguities that can generate separate names.

But in database models it’s common for people to give weird names like that to pretend they’re describing the problem well as the real name of the information (this never works out very well), so the Engines database usually accept that you use that name with space, as long as you create a way to ensure that that name is one thing, and that way is to use the backticks.

The use of normal quotes can be confused with the literal of string and would be ambiguous too.

The simple solution is to use identifiers with names that a developer understands, the user does not have access to it, no such nonsense is necessary.

See more in A Mysql query, with`crases` vs without.

I didn’t see any alias in the question.

  • Todos os Fields da view sane aliases of the table’s original name. It’s silly but with backticks work, and I believe it only works for Mysql.

  • This is not alias. Do not use names like I said and you will not have this difficulty.

  • If I wear one AS to name a field it becomes an alias, right? Yes I will not use it, it sure is a bad practice just wanted to know how the bakcticks in such a case.

  • That is, what you did is not, you just show that they are separate things. Their use is not for a type of case, it is orthogonal to all uses.

  • I feel like we’re talking about different things, I’m talking about when view that in case I did not put in question, and there I nicknamed all fields using the AS, thus leaving the view as presented in the question. but anyway this will not add to anything, what I asked you already managed to bring the answer.

  • You didn’t name anything, you created a view, you’re talking about something that doesn’t exist. If you used alias in the view and did not put in the question I can not do anything, I am talking about what you put in the question, even should have asked if it is relevant to the question, if it is not and should not have quoted it.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.