字符集是一套符号和编码的规则,不论是在oracle数据库还是在mysql数据库,都存在字符集的选择问题。对于数据库来说,字符集又是比较重要的,因为数据库存储的数
字符集是一套符号和编码的规则,不论是在oracle数据库还是在mysql数据库,都存在字符集的选择问题。对于数据库来说,字符集又是比较重要的,因为数据库存储的数据大部分都是各种文字,字符集对于数据库的存储、处理性能以及数据迁移都有重要的影响。
如果在数据库创建阶段没有正确选择字符集,那么可能在后期需要更换字符集,而字符集的更换是代价比较高的操作,也存在一定的风险,所以我们建议在应用开始阶段,就按照需求正确的选择合适的字符集,尽量避免后期不必要的调整。
mysql编译安装时,指定字符集的方法:
集和校对规则。
mysql>show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
注:如果增加default-character-set=utf8后,MYSQL启动报错。可以用character_set_server=utf8来取代default-character-set=utf8,就能正常启动了。这是因为MYSQL不同版本识别的问题。
2、数据库级
创建数据库时指定字符集
mysql>CREATE DATABASE my_db default charset utf8 COLLATE utf8_general_ci;
#注意后面这句话 "COLLATE utf8_general_ci",大致意思是在排序时根据utf8编码格式来排序
如果指定了数据库编码,那么在这个数据库下创建的所有数据表的默认字符集都会是utf8了
修改MYSQL数据库编码,如果是MYSQL数据库编码不正确,可以在MYSQL执行如下命令:
ALTER DATABASE my_db DEFAULT CHARACTER SET utf8;
以上命令就是将MYSQL的my_db数据库的编码设为utf8
3、 表级
创建表时指定字符集
mysql>create table my_table (name varchar(20) not null default '')type=myisam default charset utf8;
#这句话就是创建一个表,指定默认字符集为utf8
修改MYSQL表的编码:
ALTER TABLE my_table DEFAULT CHARACTER SET utf8;
以上命令就是将一个表my_table的编码改为utf8
4、 字段级
alter table test add column address varchar(110) after stu_id;
在stu_id后增加一个字段address
alter table test add id int unsigned not Null auto_increment primary key;
修改字段的编码:
ALTER TABLE `test` CHANGE `name` `name` VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
以上命令就是将MYSQL数据库test表中name的字段编码改为utf8
在命令行下插入汉字时如下代码:
set names utf8;有时候这一句很关键!
insert into charset values('王达');
注意:alter修改的方法不能更新已有记录的字符集,只是对新创建的表和记录生效。对已有记录字符集的调整,需要先将数据导出,经过适当调整后重新导入才可以完全修改编码。
导出导入的字符调整方法:
导出表结构
mysqldump -uroot -pmysql --default-character-set=latin1 -d my_db> createtab.sql
手工修改createtab.sql表结构定义中的字符集为新的字符集
1、导出所有记录
mysqldump -uroot -pmysql --quick --no-create-info --extended-insert --default-character-set=latin1 --host=localhost my_db> data.sql
2、打开data.sql,将set names latin1修改成set names utf8
:%s/latin1/utf8/g
全文替换
3、使用新的字符集创建新的数据库
create database mydata default charset utf8;
4、创建表,执行createtab.sql
mysql -uroot -pmysql mydata 5、导入数据 mysql -uroot -pmysql mydata 注意一点就是目标字符集要大于等于源字符集,否则会丢失一部分不支持的汉字数据。 附:旧数据升级办法 以原来的字符集为latin1为例,升级成为utf8的字符集。原来的表: old_table (default charset=latin1),新表:new_table(default charset=utf8)。 第一步:导出旧数据 mysqldump --default-character-set=latin1 -hlocalhost -uroot -B my_db --tables old_table > old.sql 第二步:转换编码 iconv -t utf8 -f latin1 -c old.sql > new.sql 在这里,假定原来的数据默认是latin1编码。 第三步:导入 修改old.sql,增加一条sql语句: "SET NAMES utf8;",保存。 mysql -hlocalhost -uroot my_db 大功告成! Mysql collate规则: *_bin: 表示的是binary case sensitive collation,也就是说是区分大小写的 本文出自 “滴水穿石孙杰” 博客,请务必保留此出处
*_cs: case sensitive collation,区分大小写
*_ci: case insensitive collation,不区分大小写

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
