MySQL SAVEPOINT語法錯誤一例

2018-07-31 19:02 更新

前幾天幫同事解決一個案例,在主從復(fù)制環(huán)境下,從庫上的MySQL版本號是5.5.5,遇到下面的錯誤:

#其他非相關(guān)信息我都隱藏掉了
 [(yejr@imysql.com)]> show slave status \G;
 Slave_IO_Running: Yes
 Slave_SQL_Running: No
 Last_Errno: 1064
 Last_Error: Error 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '6e86db84_14847168f19__8000' at line 1' on query. Default database: 'act'. Query: 'SAVEPOINT 6e86db84_14847168f19__8000'
 Last_IO_Errno: 0
 Last_IO_Error:
 Last_SQL_Errno: 1064
 Last_SQL_Error: Error 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '6e86db84_14847168f19__8000' at line 1' on query. Default database: 'act_log'. Query: 'SAVEPOINT 6e86db84_14847168f19__8000'

第一感覺是遇到保留關(guān)鍵字了,不過看到這么長的字符串,不應(yīng)該是保留關(guān)鍵字才對。
經(jīng)過嘗試,最后發(fā)現(xiàn)是字符串中的 “e” 這個字符如果存在就可能會報錯,看起來應(yīng)該是bug才對了。
在MySQL的bug系統(tǒng)里確實找到了這個bug,不過看bug描述,在5.5版本中應(yīng)該是已經(jīng)修復(fù)了才對,看來太不靠譜了呀~~
關(guān)于這個bug:Savepoint identifier is occasionally considered as floating point numbers

其實除了升級版本外,解決方法也很簡單,把savepoint后面的 identifier 字符串用反引號(波浪號的下檔鍵,英文叫做 backticks 鍵)引用起來就行。
例如:

savepoint `6e86db84_14847168f19__8000`;

這樣就可以了。

這個案例也提示我們,在寫SQL時,涉及到數(shù)據(jù)庫、表、字段、identifier 等名稱時,最好是都能用反引號引用,確??捎?。
曾經(jīng)看到線上數(shù)據(jù)表有個字段名是 check ,這個名字在MySQL里很早就已經(jīng)是保留關(guān)鍵字,幸好開發(fā)同學(xué)比較靠譜,都加上了反引號。

關(guān)于savepoint的2個bug:
Savepoint Identifier should be enclosed with backticks
Savepoint identifier is occasionally considered as floating point numbers

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號