''' mysql字符集 字符集是一套符合和編碼,校驗規則(collation)是在字符集內用於比較字符的一套規則,即字符集的排序規則。MySQL可使用對種字符集和檢驗規則來組織字符。 MySQL服務器能夠支持多種字符集,在同一臺服務器,同一個數據庫,甚至同一個表的不一樣字段均可以指定使用不一樣的字符集 相比oracle等其餘數據庫管理系統,在同一個數據庫只能使用相同的字符集,MySQL明顯存在更大的靈活性。 '''
方式1:show variables like 'character%';mysql
+--------------------------+---------------------------------------------------------------+ | Variable_name | Value | +--------------------------+---------------------------------------------------------------+ | character_set_client | utf8mb4 | | character_set_connection | utf8mb4 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | utf8mb4 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.7.17-linux-glibc2.5-x86_64/share/charsets/ | +--------------------------+---------------------------------------------------------------+
方式2:show variables like 'collation%';linux
+----------------------+--------------------+ | Variable_name | Value | +----------------------+--------------------+ | collation_connection | utf8mb4_general_ci | | collation_database | utf8mb4_general_ci | | collation_server | utf8mb4_general_ci | +----------------------+--------------------+
show charset;sql
+----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 | | dec8 | DEC West European | dec8_swedish_ci | 1 | | cp850 | DOS West European | cp850_general_ci | 1 | | hp8 | HP West European | hp8_english_ci | 1 | | koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 | | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 | | swe7 | 7bit Swedish | swe7_swedish_ci | 1 | | ascii | US ASCII | ascii_general_ci | 1 | | ujis | EUC-JP Japanese | ujis_japanese_ci | 3 | | sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 | | hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 | | tis620 | TIS620 Thai | tis620_thai_ci | 1 | | euckr | EUC-KR Korean | euckr_korean_ci | 2 | | koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 | | gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 | | greek | ISO 8859-7 Greek | greek_general_ci | 1 | | cp1250 | Windows Central European | cp1250_general_ci | 1 | | gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 | | latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 | | armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | cp866 | DOS Russian | cp866_general_ci | 1 | | keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 | | macce | Mac Central European | macce_general_ci | 1 | | macroman | Mac West European | macroman_general_ci | 1 | | cp852 | DOS Central European | cp852_general_ci | 1 | | latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 | | utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 | | cp1251 | Windows Cyrillic | cp1251_general_ci | 1 | | utf16 | UTF-16 Unicode | utf16_general_ci | 4 | | utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 | | cp1256 | Windows Arabic | cp1256_general_ci | 1 | | cp1257 | Windows Baltic | cp1257_general_ci | 1 | | utf32 | UTF-32 Unicode | utf32_general_ci | 4 | | binary | Binary pseudo charset | binary | 1 | | geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 | | cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 | | eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 | +----------+-----------------------------+---------------------+--------+
show create database game;數據庫
+----------+------------------------------------------------------------------+ | Database | Create Database | +----------+------------------------------------------------------------------+ | gamee | CREATE DATABASE `game` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ | +----------+------------------------------------------------------------------+
方式1:show table status from game like 'user_id'\G;服務器
*************************** 1. row *************************** Name: userid Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 898656 Avg_row_length: 29 Data_length: 26787840 Max_data_length: 0 Index_length: 9977856 Data_free: 5242880 Auto_increment: 1817490 Create_time: 2018-09-14 16:11:06 Update_time: 2018-09-30 17:54:12 Check_time: NULL Collation: utf8mb4_general_ci Checksum: NULL Create_options: Comment:
方式2:show create table user_id;oracle
*************************** 1. row *************************** Table: user_id Create Table: CREATE TABLE `userid` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `is_used` tinyint(4) NOT NULL DEFAULT '0', `user_id` int(11) unsigned NOT NULL, PRIMARY KEY (`id`), KEY `is_used_idx` (`is_used`) ) ENGINE=InnoDB AUTO_INCREMENT=1817490 DEFAULT CHARSET=utf8mb4 1 row in set (0.01 sec)
show full columns from 表名;ide
mysql> show full columns from recordinfo; +---------------+---------------+--------------------+------+-----+-------------------+-------+---------------------------------+---------+ | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment | +---------------+---------------+--------------------+------+-----+-------------------+-------+---------------------------------+---------+ | uuid | int(11) | NULL | NO | PRI | NULL | | select,insert,update,references | | | current | int(11) | NULL | NO | PRI | NULL | | select,insert,update,references | | | data | varchar(4096) | utf8mb4_general_ci | NO | | | | select,insert,update,references | | | replay | blob | NULL | NO | | NULL | | select,insert,update,references | | | code | int(11) | NULL | NO | MUL | NULL | | select,insert,update,references | | | time | timestamp | NULL | NO | | CURRENT_TIMESTAMP | | select,insert,update,references | | +---------------+---------------+--------------------+------+-----+-------------------+-------+---------------------------------+---------+
alter table 表名 modify `字段名` varchar(64) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '暱稱';ui
alter table 表名 default charset=utf8mb4;編碼
alter database 庫名 default character set 'utf8mb4';spa
字符集優先級由高到低:字段——表——庫
create database 庫名 default character set=utf8mb4;
create table student( id int not null auto_increment primary key, name varchar(10) not null default '' comment '姓名' ) default character set='utf8mb4';
create table student( id int not null auto_increment primary key, name varchar(10) character set utf8mb4 not null default '' comment '姓名' ) default character set='utf8mb4';
PS:
建立庫時,若是不指定字符集,則使用系統默認庫字符集
建立表時,若是不指定字符集,則繼承庫的字符集
新增字段時,若是不指定字符集,則繼承表的字符集
臨時修改(mysql服務重啓失效):
/*創建鏈接使用的編碼*/ set character_set_connection=utf8; /*數據庫的編碼*/ set character_set_database=utf8; /*結果集的編碼*/ set character_set_results=utf8; /*數據庫服務器的編碼*/ set character_set_server=utf8; set character_set_system=utf8; set collation_connection=utf8; set collation_database=utf8; set collation_server=utf8;
永久修改:
修改配置文件,重啓mysql服務
character-set-server=utf8mb4
[root@localhost ~]$ grep "utf8mb4" /etc/my.cnf character-set-server=utf8mb4
注意:以上修改,只是對新增的庫生效,已存在的庫 表 字段不生效