当前位置:网站首页>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:

      
      
# Method 1 : direct show charset
SHOW CHARSET;
# Method 2 : Inquire about information_schema Example of character_sets
SELECT * FROM information_schema.character_sets;
  • 1.
  • 2.
  • 3.
  • 4.

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:

      
      
# Method 1 : direct show collation
SHOW COLLATION;
# Method 2 : Inquire about information_schema Of collations
SELECT * FROM information_schema.collations;
  • 1.
  • 2.
  • 3.
  • 4.

Check the character code , The following is 5.7.22 Version of , Yes 222 Bar record :


MySQL Character type learning notes _ Character encoding

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


MySQL Character type learning notes _sql_02

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.
      
      
# Change the character encoding
ALTER TABLE t MODIFY COLUMN a VARCHAR(10) COLLATE utf8_bin;
# Find a piece of data , Show case sensitivity
SELECT * FROM t WHERE a = 'a';
# This situation The only index that can be built
ALTER TABLE t ADD UNIQUE KEY (a);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

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 :

      
      
# Practice of building watch
CREATE TABLE t (a CHAR(10));
# Write data
INSERT INTO t SELECT 'abcd';
# Inquire about
SELECT a, HEX(a), LENGTH(a), CHAR_LENGTH(a) FROM t;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

MySQL Character type learning notes _ Character encoding _03

Insert picture description here

      
      
# Query Global SQL_MODE
SELECT @@global.sql_mode;
# Query session SQL_MODE
SELECT @@session.sql_mode;
# Set up SQL_MODE attribute
SET SQL_MODE = 'PAD_CHAR_TO_FULL_LENGTH';
# Query again
SELECT a, HEX(a), LENGTH(a), CHAR_LENGTH(a) FROM t;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

MySQL Character type learning notes _ Sort rule _04

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

      
      
SELECT
HEX('a'),
HEX('a '),
'a' = 'a ',
BINARY('a') = BINARY('a ') ;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

MySQL Character type learning notes _ Character encoding _05

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

      
      
SET GLOBAL max_sort_length =1024;
SELECT @@global.max_sort_length;
  • 1.
  • 2.

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
      
      
CREATE INDEX indexName ON tablename(columnTest(1000));
  • 1.

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.

MySQL Character type learning notes _sql_06

Insert picture description here

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050933107333.html