【运维经】第62章——mysql字符集编码的那些事儿

运维经–目录

mysql字符集的那些事儿

  • 准备实验环境环境
MariaDB [(none)]> CREATE DATABASE test_db character set utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test_db            |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use test_db;
Database changed
MariaDB [test_db]> create table tb_books (
    ->     name varchar(45) not null,
    ->     price double not null,
    ->     bookCount int not null,
    ->     author varchar(45) not null ) default charset = utf8;
Query OK, 0 rows affected (0.05 sec)

MariaDB [test_db]> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| tb_books          |
+-------------------+
1 row in set (0.00 sec)

MariaDB [test_db]> 
  • 查看数据库字符集
MariaDB [test_db]> show variables like 'character_set_database';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| character_set_database | utf8  |
+------------------------+-------+
1 row in set (0.00 sec)
  • 查看数据表的字符集
MariaDB [test_db]> show create table tb_books;
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                             |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_books | CREATE TABLE `tb_books` (
  `name` varchar(45) NOT NULL,
  `price` double NOT NULL,
  `bookCount` int(11) NOT NULL,
  `author` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [test_db]> 
  • 修改数据库的编码格式
MariaDB [test_db]> alter database test_db character set gb2312;
Query OK, 1 row affected (0.01 sec)

MariaDB [test_db]> show variables like 'character_set_database';
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| character_set_database | gb2312 |
+------------------------+--------+
1 row in set (0.00 sec)

MariaDB [test_db]> 
  • 修改数据表格字符集
MariaDB [test_db]> show create table tb_books;
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                             |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_books | CREATE TABLE `tb_books` (
  `name` varchar(45) NOT NULL,
  `price` double NOT NULL,
  `bookCount` int(11) NOT NULL,
  `author` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [test_db]> alter table tb_books character set gb2312;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [test_db]> show create table tb_books;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                     |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_books | CREATE TABLE `tb_books` (
  `name` varchar(45) CHARACTER SET utf8 NOT NULL,
  `price` double NOT NULL,
  `bookCount` int(11) NOT NULL,
  `author` varchar(45) CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [test_db]>
  • 修改字段编码格式
MariaDB [test_db]> alter table tb_books change name name varchar(45) character set utf8 not null;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [test_db]> show create table tb_books;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                     |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_books | CREATE TABLE `tb_books` (
  `name` varchar(45) CHARACTER SET utf8 NOT NULL,
  `price` double NOT NULL,
  `bookCount` int(11) NOT NULL,
  `author` varchar(45) CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏 克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值