之前在MySQL建表和查詢的時候都用的單引號,後來發現有問題,同學提醒要用反引號,最近發現反引號也會出問題:
1.
2.
在網上看到有說,反引號是為了區分MySQL的保留字和一般字元,這樣的話第2個可以解釋,但是第1個是為什麼呢。
其實單引號和反引號的實際不同是什嗎?MySQL在對這兩個符號的操作是怎樣的?
求解,謝謝。
回複內容:
注意學習。
Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, and other object names are known as identifiers
An identifier may be quoted or unquoted. If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it.
The identifier quote character is the backtick (“`”):
MySQL :: MySQL 5.5 Reference Manual :: 9.2 Schema Object Names
A string is a sequence of bytes or characters, enclosed within either single quote (“'”) or double quote (“"”) characters
MySQL :: MySQL 5.5 Reference Manual :: 9.1.1 String Literals
最簡單的避免錯誤的方法就是,表名等保留字用反引號,其他例如SQL語句用雙引號,我平時就盡量不用單引號的簡單說字串用單引號,表名,欄位名等用反引號