当前位置:网站首页>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
边栏推荐
- Tdengine can read and write through dataX, a data synchronization tool
- Node の MongoDB Driver
- Coordinate system of view
- Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上
- 百度交易中台之钱包系统架构浅析
- 百度评论中台的设计与探索
- How to implement complex SQL such as distributed database sub query and join?
- [how to disable El table]
- Generics, generic defects and application scenarios that 90% of people don't understand
- View Slide
猜你喜欢
Why do offline stores need cashier software?
Tdengine can read and write through dataX, a data synchronization tool
mysql80服务不启动
The writing speed is increased by dozens of times, and the application of tdengine in tostar intelligent factory solution
Generics, generic defects and application scenarios that 90% of people don't understand
tongweb设置gzip
Cut off 20% of Imagenet data volume, and the performance of the model will not decline! Meta Stanford et al. Proposed a new method, using knowledge distillation to slim down the data set
百度评论中台的设计与探索
写入速度提升数十倍,TDengine 在拓斯达智能工厂解决方案上的应用
Design and exploration of Baidu comment Center
随机推荐
Mysql80 service does not start
盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名
揭秘百度智能测试在测试自动执行领域实践
Is it really reliable for AI to make complex decisions for enterprises? Participate in the live broadcast, Dr. Stanford to share his choice | qubit · viewpoint
代码语言的魅力
[how to disable El table]
卷起来,突破35岁焦虑,动画演示CPU记录函数调用过程
天龙八部TLBB系列 - 关于包裹掉落的物品
正式上架!TDengine 插件入驻 Grafana 官网
百度交易中台之钱包系统架构浅析
About getfragmentmanager () and getchildfragmentmanager ()
La voie de l'évolution du système intelligent d'inspection et d'ordonnancement des petites procédures de Baidu
What should we pay attention to when developing B2C websites?
The most comprehensive promotion strategy: online and offline promotion methods of E-commerce mall
tongweb设置gzip
Oracle combines multiple rows of data into one row of data
【OpenCV 例程200篇】219. 添加数字水印(盲水印)
Apache dolphin scheduler system architecture design
[listening for an attribute in the array]
How do enterprises choose the appropriate three-level distribution system?