1. 程式人生 > >mysql的sql_quote_show_create與SHOW CREATE TABLE命令介紹

mysql的sql_quote_show_create與SHOW CREATE TABLE命令介紹

技術背景: 剛開始學習MySQL時候,有時偷懶,會用SHOW CREATE TABLE 表名\G來複製表建立語句,可是當執行的時候總會因為"表名和列名上有單引號",提示語法錯誤不能執行。問題列表: 1,為什麼會出錯呢? 2,有什麼解決方法?解決問題: 1,分析show create table拷貝的語句出錯原因 1.1 重現過程 1.1.1 建立測試表test,並通過show create table test取得表的建立語句,可見表名,列名都用引號包著。 mysql> create table test( -> id int not null, -> primary key(id) -> ); Query OK, 0 rows affected (0.00 sec) mysql> show create table test \G *************************** 1. row *************************** Table: test Create Table: CREATE TABLE `test`

 (`id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 1 row in set (0.00 sec) 1.1.2 drop掉test表,再複製剛才的建立語句,執行後,出現預期的語法錯誤。 mysql> drop table test; Query OK, 0 rows affected (0.00 sec) mysql> Create Table: CREATE TABLE `test` ( -> `id` int(11) NOT NULL, -> PRIMARY KEY (`id`) -> ) ENGINE=MyISAM DEFAULT CHARSET=latin1 -> ;ERROR 1064 (42000): 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 ': CREATE 1.2 原理
1.2.1 如果仔細看`test`與單引號的'test'外觀上有點區別,那用字串函ASCII()來驗證一下(複製語句中`)。 從下面測試結果可知,`的ASCII碼是96, 那檢視碼錶後,才知`是"重音符",不是ASCII為39的單引號。 mysql> select ASCII('`'); +------------+ | ASCII('`') | +------------+ | 96 | +------------+ //檢視單引號的ASCII碼 mysql> select ASCII("'"); +------------+ | ASCII("'") | +------------+ | 39 | +------------+ 說明:重音符在鍵盤第二排第一個鍵,發現的時候,我表示相當尷尬。2 解決問題 2.1 利用Session設定引數set sql_quote_show_create=0;
2.1.1 sql_quote_show_create,有兩個值(1,0),預設是1,表示表名和列名會用``包著的。 這個伺服器引數只可以在session級別設定,不支援global設定的(不支援my.cnf設定)。 設定後,可見下面的沒有重音符了。 mysql> set sql_quote_show_create=0; Query OK, 0 rows affected (0.00 sec) mysql> show create table test \G *************************** 1. row *************************** Table: test Create Table: CREATE TABLE test ( id int(11) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin12.2 使用pager來處理輸出 mysql> pager tr -d '`' PAGER set to 'tr -d '`'' mysql> show create table test; +-------+------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------------------------------------------------+ | test | CREATE TABLE test ( id int(11) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |3, 命令列的使用:偷懶是一回事,如果在寫shell, python等工具的時候,可能會根據show create table來處理一些事情,即“命令列處理” 3.1 用'SET SQL_QUOTE_SHOW_CREATE=0 -bash-3.2$ mysql -uroot -e 'SET SQL_QUOTE_SHOW_CREATE=0; use test; show create table test'; +-------+------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------------------------------------------+ | test | CREATE TABLE test ( id int(11) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 | +-------+------------------------------------------------------------------------------------------------------+ 3.2 -bash-3.2$ mysql -e 'use test; show create table test \G' | tr -d '`'; *************************** 1. row *************************** Table: test Create Table: CREATE TABLE test ( id int(11) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 3.3 也可以使用sed來解決 -bash-3.2$ mysql -e 'use test; show create table test \G' | sed -e 's/`//g'; *************************** 1. row *************************** Table: test Create Table: CREATE TABLE test ( id int(11) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1