1. 程式人生 > >MySql資料庫中查詢修改表中字元編碼(charset)的方法

MySql資料庫中查詢修改表中字元編碼(charset)的方法

MySQL字符集多種多樣,下面為您列舉了其中三種最常見的MySQL字符集檢視方法,該方法供您參考,希望對您學習MySQL資料庫能有所啟迪。

一、檢視MySQL資料庫伺服器和資料庫MySQL字符集。

  1. mysql> show variables like '%char%';  
  2. +--------------------------+-------------------------------------+------  
  3. | Variable_name            | Value                               |......  
  4. +--------------------------+-------------------------------------+------  
  5. | character_set_client     | utf8                                |......   -- 客戶端字符集  
  6. | character_set_connection | utf8                                |......  
  7. | character_set_database   | utf8                                |......   -- 資料庫字符集  
  8. | character_set_filesystem | binary                              |......  
  9. | character_set_results    | utf8                                |......  
  10. | character_set_server     | utf8                                |......   -- 伺服器字符集  
  11. | character_set_system     | utf8                                |......  
  12. | character_sets_dir       | D:\MySQL Server 5.0\share\charsets\ |......  
  13. +--------------------------+-------------------------------------+------ 

二、檢視MySQL資料表(table)的MySQL字符集。

  1. mysql> show table status from sqlstudy_db like '%countries%';  
  2. +-----------+--------+---------+------------+------+-----------------+------  
  3. | Name      | Engine | Version | Row_format | Rows | Collation       |......  
  4. +-----------+--------+---------+------------+------+-----------------+------  
  5. | countries | InnoDB |      10 | Compact    |   11 | utf8_general_ci |......  
  6. +-----------+--------+---------+------------+------+-----------------+------ 

三、檢視MySQL資料列(column)的MySQL字符集。

  1. mysql> show full columns from countries;  
  2. +----------------------+-------------+-----------------+--------  
  3. | Field                | Type        | Collation       | .......  
  4. +----------------------+-------------+-----------------+--------  
  5. | countries_id         | int(11)     | NULL            | .......  
  6. | countries_name       | varchar(64) | utf8_general_ci | .......  
  7. | countries_iso_code_2 | char(2)     | utf8_general_ci | .......  
  8. | countries_iso_code_3 | char(3)     | utf8_general_ci | .......  
  9. | address_format_id    | int(11)     | NULL            | .......  
  10. +----------------------+-------------+-----------------+--------  

修改單個表的字符集:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;

修改資料庫中所有表的字符集:

SELECT concat('alter table ', table_name, ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;') FROM information_schema.tables WHERE table_schema='替換你的資料庫名稱' and table_collation != 'utf8_general_ci' GROUP BY table_name;

修改完畢後重啟資料庫:

sudo systemctl restart mariadb