MySQL查看和修改字符集的方法

一、查看字符集

1.查看MYSQL数据库服务器和数据库字符集

方法一:show variables like '%character%';
方法二:show variables like 'collation%';

2.查看MYSQL所支持的字符集

show charset;

3.查看库的字符集

语法:show database status from 库名 like  表名;

4.查看表的字符集

语法:show table status from 库名 like  表名;

mysql> show status from class_7 like 'test_info';

 

二、设置字符集

设置字符集一般有两种方法,一种是在创建表的时候设置字符集,另一种是表建成之后修改字符集。

1.创建时指定字符集

创建库的时候指定字符集:

语法:create database 库名 default character set=字符集;

create database db2 default character set=utf8

 

创建表的时候指定字符集:

语法:create table 表名(属性)default character set = 字符集;

mysql> create table test1(id int(6),name char(10)) default character set = 'gbk';
Query OK, 0 rows affected (0.39 sec)

2.修改字符集

修改全局字符集

/*建立连接使用的编码*/
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;

修改库的字符集

语法:alter database 库名 default character set 字符集;

alter database shiyan default character set gbk;

mysql> show create table test1\G
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE `test1` (
  `id` int(6) DEFAULT NULL,
  `name` char(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk   #原字符集
row in set (0.00 sec)

mysql> alter table test1 convert to character set utf8;
Query OK, 0 rows affected (0.58 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table test1\G
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE `test1` (
  `id` int(6) DEFAULT NULL,
  `name` char(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8   #修改后的字符集
row in set (0.00 sec)

修改列表的字符集

修改字段的字符集

语法:alter table 表名 modify 字段名 字段属性 character set gbk;

 alter table test1 modify name char(10) character set gbk;

mysql> show full columns from test1;
+-------+----------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type     | Collation       | Null | Key | Default | Extra | Privileges                      | Comment |
+-------+----------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| id    | int(6)   | NULL            | YES  |     | NULL    |       | select,insert,update,references |         |
| name  | char(10) | utf8_general_ci | YES  |     | NULL    |       | select,insert,update,references |         |
+-------+----------+-----------------+------+-----+---------+-------+---------------------------------+---------+
rows in set (0.01 sec)

mysql> alter table test1 modify name char(10) character set gbk;
Query OK, 0 rows affected (0.58 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show full columns from test1;
+-------+----------+----------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type     | Collation      | Null | Key | Default | Extra | Privileges                      | Comment |
+-------+----------+----------------+------+-----+---------+-------+---------------------------------+---------+
| id    | int(6)   | NULL           | YES  |     | NULL    |       | select,insert,update,references |         |
| name  | char(10) | gbk_chinese_ci | YES  |     | NULL    |       | select,insert,update,references |         |
+-------+----------+----------------+------+-----+---------+-------+---------------------------------+---------+
rows in set (0.01 sec)

修改字段的字符集

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值