当前位置:网站首页>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
边栏推荐
- About getfragmentmanager () and getchildfragmentmanager ()
- 单片机原理与接口技术(ESP8266/ESP32)机器人类草稿
- 解决Navicat激活、注册时候出现No All Pattern Found的问题
- Flutter development: a way to solve the problem of blank space on the top of listview
- Why don't you recommend using products like mongodb to replace time series databases?
- Kotlin compose and native nesting
- TDengine 连接器上线 Google Data Studio 应用商店
- How to choose the right chain management software?
- Tdengine already supports the industrial Intel edge insight package
- Resolve the horizontal (vertical) sliding conflict between viewpager and WebView
猜你喜欢
Roll up, break through 35 year old anxiety, and animate the CPU to record the function call process
Node-RED系列(二九):使用slider与chart节点来实现双折线时间序列图
解决Navicat激活、注册时候出现No All Pattern Found的问题
Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上
MySQL installation configuration and creation of databases and tables
Baidu app's continuous integration practice based on pipeline as code
Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article
How to get the STW (pause) time of GC (garbage collector)?
随机推荐
cent7安装Oracle数据库报错
How to empty uploaded attachments with components encapsulated by El upload
分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
Develop and implement movie recommendation applet based on wechat cloud
TDengine 离线升级流程
基于单片机步进电机控制器设计(正转反转指示灯挡位)
MySQL数字类型学习笔记
Comment obtenir le temps STW du GC (collecteur d'ordures)?
一文读懂TDengine的窗口查询功能
Wechat applet - simple diet recommendation (2)
Kotlin compose multiple item scrolling
解决Navicat激活、注册时候出现No All Pattern Found的问题
Node の MongoDB Driver
Why don't you recommend using products like mongodb to replace time series databases?
La voie de l'évolution du système intelligent d'inspection et d'ordonnancement des petites procédures de Baidu
Node-RED系列(二九):使用slider与chart节点来实现双折线时间序列图
一种用于干式脑电图的高密度256通道电极帽
Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)
[how to disable El table]
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution