MySQL 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=latin1
2.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
參考:
MySQL 5.1 英文文檔471頁


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM