Using backticks around field names

前端 未结 11 1322
-上瘾入骨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:23

    if you are using some field names as default mysql or mssql values for example "status", you have to use backticks ( "select status from table_name" or "select id from table_name where status=1" ). because mysql returns errors or doesnt work the query.

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

    Using backticks permits you to use alternative characters. In query writing it's not such a problem, but if one assumes you can just use backticks, I would assume it lets you get away with ridiculous stuff like

    SELECT `id`, `my name`, `another field` , `field,with,comma` 
    

    Which does of course generate badly named tables.

    If you're just being concise I don't see a problem with it, you'll note if you run your query as such

    EXPLAIN EXTENDED Select foo,bar,baz 
    

    The generated warning that comes back will have back-ticks and fully qualified table names. So if you're using query generation features and automated re-writing of queries, backticks would make anything parsing your code less confused.

    I think however, instead of mandating whether or not you can use backticks, they should have a standard for names. It solves more 'real' problems.

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

    Backticks aren't part of standard ANSI SQL. From the mysql manual:

    If the ANSI_QUOTES SQL mode is enabled, it is also allowable to quote identifiers within double quotes

    So if you use backticks and then decide to move away from MySQL, you have a problem (although you probably have a lot bigger problems as well)

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

    The only problem with backticks is that they are not ANSI-SQL compliant, e.g. they don't work in SQL Server.

    If there is a chance you would have to port your SQL to another database, use double quotes.

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

    Simple Thing about backtick `` is use for denote identifier like database_name, table_name etc, and single quote '', double quote "" for string literals, whereas "" use for print value as it is and '' print the value variable hold or in another case print the text his have.

    i.e 1.-> use `model`;   
        here `model` is database name not conflict with reserve keyword 'model'
    2- $age = 27;
    insert into `tbl_people`(`name`,`age`,`address`) values ('Ashoka','$age',"Delhi");
    
    here i used both quote for all type of requirement. If anything not clear let me know..
    
    0 讨论(0)
  • 2020-11-22 02:31

    There isn't anything wrong if you keep using MYSQL, except maybe the visual fuziness of the queries. But they do allow the use of reserved keywords or embedded spaces as table and column names. This is a no-no with most database engines and will prevent any migration at a later time.

    As for easy reading, many people use caps for SQL keywords, eg.

    SELECT some_fied, some_other_field FROM whatever WHERE id IS NULL;
    
    0 讨论(0)
提交回复
热议问题