set names 命令在mysql中除了应付乱码还能做什么?_MySQL
做一个实验,控制台进入mysql数据库,依次执行如下代码:
#创建数据库
create database it_1 charset utf8;
#进入数据库
use it_1;
#创建表
create table student(id int unsigned primary key auto_increment,stu_no char(9) not null,stu_name varchar(5) not null,stu_sex enum('男','女','保密') default '男',stu_age tinyint not null,c_id int unsigned);
#以上操作全部成功,下面的操作开始报错
#往表中插入记录
insert into student values(null,'itcast101','张三',default,18,1),(null,'itcast102','小李子',2,17,3);
结果会出现如下错误提示:
Data too long for colomn 'stu_name' at row 2#在第二行中'stu_name'字段输入的数据长度太长了
试着只运行第一行,即:
insert into student values(null,'itcast101','张三',default,18,1);#Query OK, 1 row affected
所以这里的异常现象可以描述为'stu_name'字段的预设长度为5个字符(即理论上预留了5个汉字的长度),但实际插入数据时最多只能插入2个汉字。
我马上想到了编码问题,便逐一查看各种可能影响的编码:
1.控制台的编码:gbk
2.student数据库的编码:utf-8
3.'stu_name'字段的编码可以通过如下规则确定:
a.如果字段设置了字符集就用字段设置
b.如果没有就找表字符集设置
c.如果还没有就找数据库字符集设置
d.如果还没有就找mysql系统配置的默认字符集
显然这里第c.条生效,'stu_name'字段使用utf-8字符集
我知道数据库操作中字符编码不一致会导致乱码问题,而这里控制台中的默认编码与'stu_name'字段的编码明显不一致,数据库中会不会出现乱码呢,于是我用以下三条语句逐一进行测试:
select * from student;
desc student;
show create table student;
结果是都没有出现乱码。由于目前相关知识有限,于是我就凌乱了。。。
通过请教朋友有后,得到了这样的解决方法:
在建表前加入一条命令
set names gbk;
这条命令的作用是告知mysql服务器,从客户端传来的(这里是控制台输入的)信息采用字符集gbk,而且从mysql服务器发送回客户端的结果也要用字符集gbk。
如果不使用这个命令,那么控制台输入的信息实际采用字符集gbk,但mysql服务器误认为是utf-8字符集,不进行转换就接收了。由于utf-8字符集里中文使用三个字节来编码英文只用1个字节,gbk字符集里中、英文都是使用2个字节来编码,于是控制台传递过去的3个gbk码中文字符(6字节)没有被mysql服务器识别为中文,而是直接识别成了6个英文字符,所以超出了最长5个字符的字段长度限制,导致此处的错误出现。
set names 命令能做的原来不只是应付乱码问题。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.
