2017-07-31 70 views
1

MySQL什麼是以編程方式檢索字符集和當前數據庫排序規則的最佳方式?確定MySQL中的默認數據庫字符集和排序規則

如下:

SELECT 
    default_character_set_name, default_collation_name 
FROM 
    information_schema.SCHEMATA 
WHERE 
    SCHEMA_NAME = SCHEMA() 

等同於下面的例子?

select @@character_set_database, @@collation_database 

回答

1

根據文檔:

character_set_database

...

被默認 數據庫使用的字符集。只要默認數據庫 更改,服務器就會設置此變量。如果沒有默認數據庫,則該變量與character_set_server具有相同的 值。

...

collation_database

...

由默認的數據庫使用的排序規則。只要默認數據庫發生更改, 服務器就會設置此變量。如果 沒有默認數據庫,則該變量具有與 collation_server相同的值。

...

與兩個句子將獲得相同的結果:

SELECT 
    default_character_set_name, default_collation_name 
FROM 
    information_schema.SCHEMATA 
WHERE 
    SCHEMA_NAME = SCHEMA() 

select @@character_set_database, @@collation_database 

在下面的測試證明:

mysql> DROP DATABASE IF EXISTS `my_database`; 
Query OK, 0 rows affected (0.01 sec) 

mysql> SELECT SCHEMA(); 
+----------+ 
| SCHEMA() | 
+----------+ 
| NULL  | 
+----------+ 
1 row in set (0.00 sec) 

mysql> SELECT 
    -> @@SESSION.character_set_database, 
    -> @@SESSION.collation_database, 
    -> @@SESSION.character_set_server, 
    -> @@SESSION.collation_server; 
+----------------------------------+------------------------------+--------------------------------+----------------------------+ 
| @@SESSION.character_set_database | @@SESSION.collation_database | @@SESSION.character_set_server | @@SESSION.collation_server | 
+----------------------------------+------------------------------+--------------------------------+----------------------------+ 
| latin1       | latin1_swedish_ci   | latin1       | latin1_swedish_ci   | 
+----------------------------------+------------------------------+--------------------------------+----------------------------+ 
1 row in set (0.00 sec) 

mysql> CREATE DATABASE IF NOT EXISTS `my_database` 
    -> CHARACTER SET utf8mb4 
    -> COLLATE utf8mb4_general_ci; 
Query OK, 1 row affected (0.00 sec) 

mysql> SELECT SCHEMA(); 
+----------+ 
| SCHEMA() | 
+----------+ 
| NULL  | 
+----------+ 
1 row in set (0.00 sec) 

mysql> SELECT 
    -> @@SESSION.character_set_database, 
    -> @@SESSION.collation_database, 
    -> @@SESSION.character_set_server, 
    -> @@SESSION.collation_server; 
+----------------------------------+------------------------------+--------------------------------+----------------------------+ 
| @@SESSION.character_set_database | @@SESSION.collation_database | @@SESSION.character_set_server | @@SESSION.collation_server | 
+----------------------------------+------------------------------+--------------------------------+----------------------------+ 
| latin1       | latin1_swedish_ci   | latin1       | latin1_swedish_ci   | 
+----------------------------------+------------------------------+--------------------------------+----------------------------+ 
1 row in set (0.00 sec) 

mysql> USE `my_database`; 
Database changed 

mysql> SELECT SCHEMA(); 
+-------------+ 
| SCHEMA() | 
+-------------+ 
| my_database | 
+-------------+ 
1 row in set (0.00 sec) 

mysql> SELECT 
    -> @@SESSION.character_set_database, 
    -> @@SESSION.collation_database, 
    -> @@SESSION.character_set_server, 
    -> @@SESSION.collation_server; 
+----------------------------------+------------------------------+--------------------------------+----------------------------+ 
| @@SESSION.character_set_database | @@SESSION.collation_database | @@SESSION.character_set_server | @@SESSION.collation_server | 
+----------------------------------+------------------------------+--------------------------------+----------------------------+ 
| utf8mb4       | utf8mb4_general_ci   | latin1       | latin1_swedish_ci   | 
+----------------------------------+------------------------------+--------------------------------+----------------------------+ 
1 row in set (0.00 sec) 

mysql> SELECT 
    -> `DEFAULT_CHARACTER_SET_NAME`, 
    -> `DEFAULT_COLLATION_NAME` 
    -> FROM 
    -> `information_schema`.`SCHEMATA` 
    -> WHERE 
    -> SCHEMA_NAME = SCHEMA(); 
+----------------------------+------------------------+ 
| DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | 
+----------------------------+------------------------+ 
| utf8mb4     | utf8mb4_general_ci  | 
+----------------------------+------------------------+ 
1 row in set (0.00 sec) 

然而,這兩個句子的結合不會是錯的:

mysql> USE `my_database`; 
Database changed 

mysql> SELECT 
    -> `DEFAULT_CHARACTER_SET_NAME`, 
    -> `DEFAULT_COLLATION_NAME` 
    -> FROM 
    -> `information_schema`.`SCHEMATA` 
    -> WHERE 
    -> SCHEMA_NAME = SCHEMA() AND 
    -> `DEFAULT_CHARACTER_SET_NAME` = @@SESSION.character_set_database AND 
    -> `DEFAULT_COLLATION_NAME` = @@SESSION.collation_database; 
+----------------------------+------------------------+ 
| DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | 
+----------------------------+------------------------+ 
| utf8mb4     | utf8mb4_general_ci  | 
+----------------------------+------------------------+ 
1 row in set (0.00 sec) 
相關問題