MySQL——修改数据库和表的字符编码

修改编码:

(1)先停止服务

(2)修改my.ini文件

(3)重新启动服务说明:

如果是在修改my.ini之前建的库和表,那么库和表的编码还是原来的Latin1,要么删了重建,要么使用

alter语句修改编码。

|------------------------------------------------------------------------------------|
| mysql> create database 0728db charset Latin1; Query OK, 1 row affected (0.00 sec) |

|--------------------------------------|
| mysql> use 0728db; Database changed |

|---|---|
| |
| | |

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| mysql> alter table student charset utf8; #修改表字符编码为UTF8 Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table student\G *************************** 1. row *************************** Table: student Create Table: CREATE TABLE `student` ( `id` int(11) NOT NULL, `name` varchar(20) CHARACTER SET latin1 DEFAULT NULL, #字段仍然是latin1编码PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) mysql> alter table student modify name varchar(20) charset utf8; #修改字段字符编码为UTF8 Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table student\G *************************** 1. row *************************** Table: student Create Table: CREATE TABLE `student` ( `id` int(11) NOT NULL, `name` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) |

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| mysql> show create database 0728db;; +--------+-----------------------------------------------------------------+ |Database| Create Database | +------+-------------------------------------------------------------------+ |0728db| CREATE DATABASE `0728db` /*!40100 DEFAULT CHARACTER SET latin1 */ | +------+-------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> alter database 0728db charset utf8; #修改数据库的字符编码为utf8 Query OK, 1 row affected (0.00 sec) |

|---|---|
| |
| | |

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| mysql> create table student (id int , name varchar(20)) charset Latin1; Query OK, 0 rows affected (0.02 sec) mysql> show create table student\G *************************** 1. row *************************** Table: student Create Table: CREATE TABLE `student` ( `id` int(11) NOT NULL, `name` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) |

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| mysql> show create database 0728db; +--------+-----------------------------------------------------------------+ |Database| Create Database | +--------+-----------------------------------------------------------------+ | 0728db | CREATE DATABASE `0728db` /*!40100 DEFAULT CHARACTER SET utf8 */ | +--------+-----------------------------------------------------------------+ 1 row in set (0.00 sec) |

相关推荐
洪小帅13 分钟前
Django的models.model如何使用
数据库·python·django·sqlite
程序员入门中1 小时前
深入理解 @Transactional 注解与 Spring 事务机制
java·数据库·spring
MrZhangBaby1 小时前
SQL-leetcode—1193. 每月交易 I
数据库·sql·leetcode
m0_548514771 小时前
Microsoft SQL Serve的下载与安装
数据库·sql·microsoft
马船长1 小时前
ASP.NET代码审计 SQL注入篇(简单记录)
开发语言·数据库·sql·网络安全·asp.net
AI航海家(Ethan)1 小时前
django使用踩坑经历
数据库·postgresql·django·sqlite
nullptroot1 小时前
docker配置mysql并使用mysql connector cpp编程
mysql·docker
m0_748245741 小时前
如何在docker中的mysql容器内执行命令与执行SQL文件
sql·mysql·docker
有杨既安然1 小时前
Docker 从零开始掌握容器化技术
linux·运维·数据库·ubuntu·docker·容器·负载均衡
洪小帅2 小时前
Django实现数据库的表间三种关系
数据库·python·django·sqlite