当前位置:网站首页>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
边栏推荐
- Windows uses commands to run kotlin
- Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下
- Kotlin Compose 与原生 嵌套使用
- Small program startup performance optimization practice
- 揭秘百度智能测试在测试自动执行领域实践
- Tdengine already supports the industrial Intel edge insight package
- [technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode
- ThreadLocal source code learning
- Android SQLite database encryption
- Community group buying has triggered heated discussion. How does this model work?
猜你喜欢
Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes
Cross process communication Aidl
Idea debugs com intellij. rt.debugger. agent. Captureagent, which makes debugging impossible
QT event filter simple case
Charm of code language
【技术直播】如何用 VSCode 从 0 到 1 改写 TDengine 代码
.Net之延迟队列
Tdengine connector goes online Google Data Studio app store
基于宽表的数据建模应用
How to implement complex SQL such as distributed database sub query and join?
随机推荐
Community group buying has triggered heated discussion. How does this model work?
The popularity of B2B2C continues to rise. What are the benefits of enterprises doing multi-user mall system?
百度智能小程序巡檢調度方案演進之路
搞数据库是不是越老越吃香?
Idea debugs com intellij. rt.debugger. agent. Captureagent, which makes debugging impossible
盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名
为什么不建议你用 MongoDB 这类产品替代时序数据库?
Android SQLite database encryption
Tdengine offline upgrade process
ArcGIS Pro 创建要素
Openes version query
硬核,你见过机器人玩“密室逃脱”吗?(附代码)
Kotlin Compose 多个条目滚动
Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
Officially launched! Tdengine plug-in enters the official website of grafana
观测云与 TDengine 达成深度合作,优化企业上云体验
Resolve the horizontal (vertical) sliding conflict between viewpager and WebView
Cross process communication Aidl
Wechat applet - simple diet recommendation (2)
90%的人都不懂的泛型,泛型的缺陷和应用场景