Using backticks around field names

前端 未结 11 1300
-上瘾入骨i
-上瘾入骨i 2020-11-22 02:05

After reading a couple of answers and comments on some SQL questions here, and also hearing that a friend of mine works at a place which has a policy which bans them, I\'m w

相关标签:
11条回答
  • 2020-11-22 02:38

    The main use of backticks (`) in SQL is to use them in situations where you are going to call them again in upcoming clauses. In every other time it is recommended to use double quotes("").

    For example

    SELECT CONCAT(Name, ' in ', city, ', ', statecode) AS `Publisher and Location`,
        COUNT(ISBN) AS "# Books",
        MAX(LENGTH(title)) AS "Longest Title",
        MIN(LENGTH(title)) AS "Shortest Title"
    FROM Publisher JOIN Book
    ON Publisher.PublisherID = Book.PublisherID WHERE INSTR(name, 'read')>0
    GROUP BY `Publisher and Location`
    HAVING COUNT(ISBN) > 1;
    

    In the above statement do you see how Publisher and Location is used again in GROUP BY clause.

    Instead of using

    GROUP BY Name, city, statecode

    I just used

    GROUP BY Publisher and Location

    Only when such situations arise, it is useful to use backticks. In all other times using double quotes is recommended.

    0 讨论(0)
  • 2020-11-22 02:41

    To me it makes a lot of sense to use them at all times when dealing with field names.

    • Firstly, once you get into the habit, it doesn't hurt to just hit the backtick key.
    • Secondly, to me, it makes it easier to see what exactly are the fields in your query, and what are keywords or methods.
    • Lastly, it allows you to use whatever field name you wish when designing your table. Sometimes it makes a lot of sense to name a field "key", "order", or "values"... all of which require backticks when referring to them.
    0 讨论(0)
  • 2020-11-22 02:44

    If you ask to me, backticks should always be used. But there are some reasons why a team may prefer not to use them.

    Advantages:

    • Using them, there are no reserved words or forbidden chars.
    • In some cases, you get more descriptive error messages.
    • If you avoid bad practices you don't care, but... in real word, sometimes they are a decent way to avoid SQL injections.

    Disadvantages:

    • They are not standard and usually not portable. However, as long as you don't use a backtick as part of an identifier (which is the worst practice I am able to imagine), you can port your query by automatically removing backticks.
    • If some of your query come from Access, they may quote table names with " (and maybe you can't remove all the " blindly). However, mixtures of backticks and double quotes are allowed.
    • Some stupid software or function filters your queries, and has problems with backticks. However, they are part of ASCII so this means that your software/function is very bad.
    0 讨论(0)
  • 2020-11-22 02:49

    Well, as far as I know, the whole purpose of using backticks is so you can use names that coincide with reserved keywords. So, if the name isn't colliding with a reserved keyword, I don't see any reason to use backticks. But, that's no reason to ban them, either.

    0 讨论(0)
  • 2020-11-22 02:50

    It's a lot easier to search your code-base for something in backticks. Say you have a table named event. grep -r "event" * might return hundreds of results. grep -r "\`event\`" * will return anything probably referencing your database.

    0 讨论(0)
提交回复
热议问题