当前位置:网站首页>MySQL character type learning notes
MySQL character type learning notes
2022-07-05 10:04:00 【51CTO】
@[toc]
One 、 Character set and character encoding
1.1、 Character set
- Character set : A character set is a set of characters , for example GB2312 Is the simplified Chinese character set of Chinese national standard ,GB2312 Include simplified Chinese characters (6763 individual ) And general symbols 、 Serial number 、 Numbers 、 Latin alphabet 、 Japanese kana 、 The Greek letter 、 Russian letters 、 Chinese pinyin symbols 、 Chinese phonetic alphabet , common 7445 Graphic characters .
Character set query sql:
I'm a local 5.7.22 edition , The supported character sets are as follows :
Charset | Description | Default collation | Maxlen |
big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
dec8 | DEC West European | dec8_swedish_ci | 1 |
cp850 | DOS West European | cp850_general_ci | 1 |
hp8 | HP West European | hp8_english_ci | 1 |
koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
latin1 | cp1252 West European | latin1_swedish_ci | 1 |
latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
ascii | US ASCII | ascii_general_ci | 1 |
ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
tis620 | TIS620 Thai | tis620_thai_ci | 1 |
euckr | EUC-KR Korean | euckr_korean_ci | 2 |
koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
greek | ISO 8859-7 Greek | greek_general_ci | 1 |
cp1250 | Windows Central European | cp1250_general_ci | 1 |
gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
cp866 | DOS Russian | cp866_general_ci | 1 |
keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
macce | Mac Central European | macce_general_ci | 1 |
macroman | Mac West European | macroman_general_ci | 1 |
cp852 | DOS Central European | cp852_general_ci | 1 |
latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |
cp1256 | Windows Arabic | cp1256_general_ci | 1 |
cp1257 | Windows Baltic | cp1257_general_ci | 1 |
utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
binary | Binary pseudo charset | binary | 1 |
geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
1.2、 Character encoding
- Character encoding : Character encoding is to map characters to specific bytes or byte sequences , But generally, a specific character set adopts a specific encoding method
Character encoding query sql:
Check the character code , The following is 5.7.22 Version of , Yes 222 Bar record :

Insert picture description here
Be careful :MySQL The whole table can be set if the character set of is not set , It can also be detailed to specific fields , The usage is to add
charset [ Character set name ]
Two 、 Character set collation
2.1、 Collation definition
Sort rule (Collation): Sorting rules refer to the comparison rules of characters under character set .
2.2 、 Collation features
Collation features :
- Each character set has a default collation
- A character set corresponds to a collation , Two different character sets cannot have the same collation
Be careful : The common naming rules for sorting rules are : With _ci The ending is not case sensitive (case insensitive), With _cs The ending is case sensitive (case sensitive), With _bin The ending represents a binary comparison (binary)
Pictured , Choose one. collation,5.7.22 Version of , Most of them are ci At the end of the , That is, the case is not sensitive

Insert picture description here
Example :
# Build table , Character encoding defaults to utf8_general_ci
CREATE TABLE t (
a VARCHAR(10)
)CHARSET = utf8;
# Write two pieces of data
INSERT INTO t SELECT 'a';
INSERT INTO t SELECT 'A';
# Find out two pieces of data , It means that case is not sensitive
SELECT * FROM t WHERE a='a';
# The only index can't be built
ALTER TABLE t ADD UNIQUE KEY (a);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
3、 ... and 、CHAR and VARCHAR
char and varchar Is the two most common string types , The grammar is char(N) and varchar(N), Pay attention to N stay MySQL4.1 After the version, it indicates the length of the characters , Not byte length , stay MySQL4.1 The previous version only represents the length of bytes
3.1、CHAR type
about CHAR(N),N For the range of 0~255
CHAR(N) To save a fixed length string , Also according to the set value N, If N Set to 10, No matter what range of strings you pass , It's all fixed length 10 Of , Because the database populates the right side of the storage column (Right padded), When reading, the database will automatically delete the filled characters . Unless set SQL_MODE attribute PAD_CHAR_TO_FULL_LENGTH, Can display normally
Example practice :

Insert picture description here

Insert picture description here
3.2、VARCHAR type
about varchar Type is used to store variable length strings , It means varchar(10) The column of , I write strings “abcd“, This column stores 4 Characters , and char(10) The columns of are stored 10 Characters
Four 、BINARY and VARBINARY
4.1、 Feature comparison
BINARY(N) and VARBINARY(N) And CHAR(N) and VARCHAR(N) The type is a bit similar ,BINARY Analogical CHAR,VARBINARY By analogy VARCHAR type .
contrast :
- The difference is BINARY and VARBINARY All of them are binary strings ,CHAR and VARCHAR What is stored is a string of character type
- BINARY and VARBINARY There is no concept of character set ,CHAR and VARCHAR These are the concepts of character sets
- BINARY(N) and VARBINARY(N) Medium N The length of the bytes represented , We know from the front that CHAR(N) and VARCHAR(N) Medium N since MySQL4.1 after , It means the length of the character
- CHAR and VARCHAR When comparing characters , Is to compare the characters stored in itself , Ignore filled characters , and BINARY and VARBINARY Is not ignored , The comparison is binary
4.2、 Example practice

Insert picture description here
5、 ... and 、BLOB and TEXT
Can be blob A column of type is considered large enough varbinary Column of type , Can also be text A column of type is considered large enough varchar Column of type
5.1、text type
TEXT It is used to store character big data type ,TEXT The type and blob Same type , Can be subdivided into :
- tinytext(2^8)
- text(2^16)
- mediumtext(2^24)
- longtext(2^32)
5.2、blob type
BLOB(Binary Large Object) It is used to store binary big data type . Depending on the storage length ,blob The types can be subdivided into :
- tinyblob(2^8)
- blob(2^16)
- mediumblob(2^24)
- longblob(2^32)
5.3、 Scheduling problem
blob and text Scheduling problem :
Be careful : BLOB and TEXT Only the first... Of the column is used for sorting max_sort_length Bytes
5.4、 Index problem
blob and text The question of the Picasso :
blob and text Type of column plus index , Need to pay attention to some details
- 1、 There cannot be default values for columns
- 2、 You need to specify the length of index prefix when adding index
6、 ... and 、ENUM and SET type
6.1、 Introduction to set types
enum and set All types are set types , The difference is enum Enumerate at most 65536 Elements , and set Type enumeration at most 64 Elements
6.2、 Examples of set types
# Create table validation problem
CREATE TABLE t (username VARCHAR(20),sex ENUM('male','female'))ENGINE=INNODB;
# Write data normally
INSERT INTO t SELECT 'David','male';
INSERT INTO t SELECT 'john','female';
# Writing data , Because there is no corresponding enumeration type , But it can still write , It's not a warning
INSERT INTO t SELECT 'mariah','security';
# Find out , Find out sex Field is not worth
select * from t ;
# Set strict mode
SET SQL_MODE = 'strict_trans_tables';
# Write the data again , You can't write
INSERT INTO t SELECT 'mariah','security';
# Query again failed to write successfully
SELECT * FROM t;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.

Insert picture description here
边栏推荐
- 能源势动:电力行业的碳中和该如何实现?
- 【OpenCV 例程200篇】219. 添加数字水印(盲水印)
- [hungry dynamic table]
- Android SQLite database encryption
- Getting started with Apache dolphin scheduler (one article is enough)
- How Windows bat script automatically executes sqlcipher command
- Tongweb set gzip
- On July 2, I invite you to TD Hero online press conference
- The popularity of B2B2C continues to rise. What are the benefits of enterprises doing multi-user mall system?
- 如何获取GC(垃圾回收器)的STW(暂停)时间?
猜你喜欢

代码语言的魅力

基于宽表的数据建模应用

Three-level distribution is becoming more and more popular. How should businesses choose the appropriate three-level distribution system?

从“化学家”到开发者,从甲骨文到 TDengine,我人生的两次重要抉择

Tdengine connector goes online Google Data Studio app store

如何获取GC(垃圾回收器)的STW(暂停)时间?

百度APP 基于Pipeline as Code的持续集成实践

QT event filter simple case

MySQL installation configuration and creation of databases and tables

Why don't you recommend using products like mongodb to replace time series databases?
随机推荐
分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
QT realizes signal transmission and reception between two windows
[app packaging error] to proceed, either fix the issues identified by lint, or modify your build script as follow
Oracle combines multiple rows of data into one row of data
盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名
E-commerce apps are becoming more and more popular. What are the advantages of being an app?
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下
搞数据库是不是越老越吃香?
The writing speed is increased by dozens of times, and the application of tdengine in tostar intelligent factory solution
Meitu lost 300 million yuan in currency speculation for half a year. Huawei was exposed to expand its enrollment in Russia. Alphago's peers have made another breakthrough in chess. Today, more big new
On July 2, I invite you to TD Hero online press conference
Are databases more popular as they get older?
[how to disable El table]
百度智能小程序巡检调度方案演进之路
tongweb设置gzip
[sorting of object array]
Fluent development: setting method of left and right alignment of child controls in row
Node-RED系列(二九):使用slider与chart节点来实现双折线时间序列图
从“化学家”到开发者,从甲骨文到TDengine,我人生的两次重要抉择