2017-03-07 44 views
0

我正在使用MySQL 5.5.37。對於我的數據庫默認的編碼是UTF8 ...如何創建我的MySQL視圖尊重與數據庫的默認相同的字符編碼?

mysql> SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = "my_db"; 
+----------------------------+ 
| default_character_set_name | 
+----------------------------+ 
| utf8      | 
+----------------------------+ 
1 row in set (0.00 sec) 

然而,當我創建一個使用

CREATE OR REPLACE VIEW report_toc_item AS 
SELECT ti.*, pti.type_id PARENT_TYPE_ID 
      FROM toc_item ti 
      JOIN toc_item pti ON pti.id = ti.parent_id 
      WHERE ti.type_id = 'sub_segment'; 

,然後我表明,圖的圖中,cndoing被顯示爲「latin1的」,而不是utf8 ...

mysql> show create view report_toc_item; 
+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ 
| View    | Create View                                                                                                                                                                                                                              | character_set_client | collation_connection | 
+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ 
| report_toc_item | CREATE ALGORITHM=UNDEFINED DEFINER=`mytestuser`@`localhost` SQL SECURITY DEFINER VIEW `report_toc_item` AS select `ti`.`ID` AS `ID`,`ti`.`PARENT_ID` AS `PARENT_ID`,`ti`.`TOC_ID` AS `TOC_ID`,`ti`.`TITLE` AS `TITLE`,`ti`.`DESCRIPTION` AS `DESCRIPTION`,`ti`.`TYPE_ID` AS `TYPE_ID`,`ti`.`ORDER_NUM` AS `ORDER_NUM`,`ti`.`MY_OBJECT_SEGMENT_ID` AS `MY_OBJECT_SEGMENT_ID`,`ti`.`MY_OBJECT_SEGMENT_ORDER_NUM` AS `MY_OBJECT_SEGMENT_ORDER_NUM`,`ti`.`ELEMENT_ID` AS `ELEMENT_ID`,`ti`.`UNIT_TOC_ITEM_ID` AS `UNIT_TOC_ITEM_ID`,`ti`.`SHORT_NAME` AS `SHORT_NAME`,`ti`.`THIRD_PARTY_PROMPT_ID` AS `THIRD_PARTY_PROMPT_ID`,`pti`.`TYPE_ID` AS `PARENT_TYPE_ID` from (`toc_item` `ti` join `toc_item` `pti` on((`pti`.`ID` = `ti`.`PARENT_ID`))) where ((`ti`.`TYPE_ID` = 'sub_segment') and ((`pti`.`TYPE_ID` = 'lesson') or (`pti`.`TYPE_ID` = 'activity') or (`pti`.`TYPE_ID` = 'activity_practice') or (`pti`.`TYPE_ID` = 'unit_opener'))) | latin1    | latin1_swedish_ci | 
+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ 
1 row in set (0.00 sec) 

如何讓我的視圖尊重數據庫的默認編碼?我寧願不將編碼硬編碼到視圖的創建中。

回答

0
SHOW CREATE DATABASE mydb; -- to see what the database's charset is 
SHOW VIEW x; -- to get the body 
DROP VIEW x; 
SET NAMES utf8mb4; -- or whatever you got from the first step 
CREATE VIEW ... -- recreate the view. 

是的,笨拙。

+0

有什麼辦法可以使「SET NAMES utf8mb4;」創建數據庫的一部分,而不是在創建數據庫之後運行它? – Dave

+0

您的評論與您的​​問題不符。 'CREATE DATABASE ... CHARACTER SET ...'適用於_database_創建。 'SET NAMES ... CREATE VIEW ...'適用於_view_創建。我同意這似乎是'錯誤'。 –

相關問題