跟我一起学习MySQL技术内幕(第五版):(第三章学习日记2上)
3.1.2字符串值 3.1.2.1字符串类型与字符集支持 3.1.2.2字符集相关的系统变量 3.1.2.1字符串类型与字符集支持 字符串值一般可以分为两类,二进制串和非二进制串 二进制串:一组字节序列,没有特殊的比较或者排序属性. 比较操作是基于各字节的数值逐个字节实现的.
3.1.2字符串值
3.1.2.1字符串类型与字符集支持
3.1.2.2字符集相关的系统变量
3.1.2.1字符串类型与字符集支持
字符串值一般可以分为两类,二进制串和非二进制串
<code> 二进制串:一组字节序列,没有特殊的比较或者排序属性. 比较操作是基于各字节的数值逐个字节实现的. 所有字节都有意义,**甚至包括结尾的空格**. 非二进制串:一个字符序列,每个二进制串都与字符集相关. 字符集决定了:MySQL如何解释字符串内容,哪些字符可以用. 每个字符集都有一种或者多种排序规则. **尾部空格不会参与比较(TEXT类型除外-具有唯一性索引)** </code>
字符串所使用的排序规则决定了字符在字符集里的先后顺序,这会对比较操作产生影响.
默认的字符集和排序规则分别为latin1和latin_swedish_ci.
查看服务器上提供的字符集以及排序规则:
<code class=" hljs smalltalk">show character set; +----------+-----------------------------+---------------------+--------+ | <span class="hljs-class">Charset</span> | <span class="hljs-class">Description</span> | <span class="hljs-class">Default</span> collation | <span class="hljs-class">Maxlen</span> | +----------+-----------------------------+---------------------+--------+ <span class="hljs-localvars">| big5 |</span> <span class="hljs-class">Big5</span> <span class="hljs-class">Traditional</span> <span class="hljs-class">Chinese</span> <span class="hljs-localvars">| big5_chinese_ci |</span> <span class="hljs-number">2</span> | <span class="hljs-localvars">| dec8 |</span> <span class="hljs-class">DEC</span> <span class="hljs-class">West</span> <span class="hljs-class">European</span> <span class="hljs-localvars">| dec8_swedish_ci |</span> <span class="hljs-number">1</span> | <span class="hljs-localvars">| cp850 |</span> <span class="hljs-class">DOS</span> <span class="hljs-class">West</span> <span class="hljs-class">European</span> <span class="hljs-localvars">| cp850_general_ci |</span> <span class="hljs-number">1</span> | <span class="hljs-localvars">| hp8 |</span> <span class="hljs-class">HP</span> <span class="hljs-class">West</span> <span class="hljs-class">European</span> <span class="hljs-localvars">| hp8_english_ci |</span> <span class="hljs-number">1</span> | <span class="hljs-localvars">| koi8r |</span> <span class="hljs-class">KOI8</span>-<span class="hljs-class">R</span> <span class="hljs-class">Relcom</span> <span class="hljs-class">Russian</span> <span class="hljs-localvars">| koi8r_general_ci |</span> <span class="hljs-number">1</span> | ....... ....... show collation; --------------------------+----------+-----+---------+----------+---------+ | <span class="hljs-class">Collation</span> | <span class="hljs-class">Charset</span> | <span class="hljs-class">Id</span> | <span class="hljs-class">Default</span> | <span class="hljs-class">Compiled</span> | <span class="hljs-class">Sortlen</span> | +--------------------------+----------+-----+---------+----------+---------+ <span class="hljs-localvars">| big5_chinese_ci | big5 |</span> <span class="hljs-number">1</span> | <span class="hljs-class">Yes</span> | <span class="hljs-class">Yes</span> | <span class="hljs-number">1</span> | <span class="hljs-localvars">| big5_bin | big5 |</span> <span class="hljs-number">84</span> | | <span class="hljs-class">Yes</span> | <span class="hljs-number">1</span> | <span class="hljs-localvars">| dec8_swedish_ci | dec8 |</span> <span class="hljs-number">3</span> | <span class="hljs-class">Yes</span> | <span class="hljs-class">Yes</span> | <span class="hljs-number">1</span> | <span class="hljs-localvars">| dec8_bin | dec8 |</span> <span class="hljs-number">69</span> | | <span class="hljs-class">Yes</span> | <span class="hljs-number">1</span> | <span class="hljs-localvars">| cp850_general_ci | cp850 |</span> <span class="hljs-number">4</span> | <span class="hljs-class">Yes</span> | <span class="hljs-class">Yes</span> | <span class="hljs-number">1</span> | <span class="hljs-localvars">| cp850_bin | cp850 |</span> <span class="hljs-number">80</span> | | <span class="hljs-class">Yes</span> | <span class="hljs-number">1</span> | ........ ........</code>
每种排序规则都捆绑在某个特定的字符集上,而每个给定的字符集可以有多种排序规则.
格式: 字符集名语言名附加后缀
后缀规则如下:
_ci表示排序规则不区分大小写
_cs表示排序规则要区分大小写
_bin表示这是一种二进制排序规则.(比较操作基于数字字符编码值进行,与语言无关)
如:utf8_bin
二进制串和非二进制串的排序特性:
二: 逐字节进行比较,结果只取决于每个字节的数值大小,区分大小写(大小写不同,对应
字节数值不同,二进制串其实并没有大小写的概念区分大小写实际上是排序规则的一项功能)
非二:按字符进行比较,每一个字符的相对值取决于当前所用字符集的排序规则.大小写设定为同一排序值,所以不区分大小写(不适用于区分大小写的非二进制排序)
确定某个字符串的字符集和排序规则:
(默认情况下,MySQL会把十六进制常量当作二进制串对待)
<code class=" hljs asciidoc">select charset(x'0123'),collation(x'0123'); <span class="hljs-code">+------------------+</span>--------------------+ <span class="hljs-header">| charset(x'0123') | collation(x'0123') | +------------------+--------------------+</span> <span class="hljs-header">| binary | binary | +------------------+--------------------+</span> </code>
有两种记法约定可以用于将某个字符串强制解释为某种指定的字符集.
1._charset str
<code class=" hljs bash">_latin2 <span class="hljs-string">'abc'</span> _latin2 x<span class="hljs-string">'616263'</span> _latin2 <span class="hljs-number">0</span>x616263 _utf8 <span class="hljs-string">'def'</span> _utf8 X<span class="hljs-string">'646566'</span> _utf8 <span class="hljs-number">0</span>x646566</code>
<code>对于引号里的字符串: 字符集引导符与字符串之间空白可选 十六进制不能留有任何空白 </code>
2.N’str’(等价于_utf8’str’)
N的后面必须紧跟一个引号形式的字符串,不能有任何空白
(3.对于字符串表达式或列值的引导符记法)
<code class=" hljs cs">convert (str <span class="hljs-keyword">using</span> charset);</code>
引导符和convert是不一样的,引导符只会改变对字符串的解释,不会改变值,而convert是一个函数,进入的是输入参数,生成一个新的字符串返回.
<code class=" hljs asciidoc">set @s1 = <span class="hljs-emphasis">_ucs2 'ABCD'; set @s2 = convert ('ABCD' using ucs2); select char_</span>length(@s1), length(@s1), char<span class="hljs-emphasis">_length(@s2), length(@s2); </span> <span class="hljs-code">+------------------+</span>-------------<span class="hljs-code">+------------------+</span>-------------+ <span class="hljs-header">| char_length(@s1) | length(@s1) | char_length(@s2) | length(@s2) | +------------------+-------------+------------------+-------------+</span> <span class="hljs-header">| 2 | 4 | 4 | 8 | +------------------+-------------+------------------+-------------+</span> </code>
第一条语句:把ABCD每一对字符解释为一个双字节ucs2字符
第二条语句:把每个字符转化为相应的ucs2字符.
这一节最后介绍了二进制串与使用二进制串排序规则的非二进制串的区别.
1.二进制串没有字符集的概念.它会被解释为字节,并且比较的是单字节的数字代码
2.使用了二进制排序规则的非二进制串,会被解释为字符,并且比较的是它们的数字字符值,这种值通常是基于每个字符多个字节算出的.
<code class=" hljs asciidoc">set @s1 = binary <span class="hljs-emphasis">'abcd'</span>; set @s2 = <span class="hljs-emphasis">_latin1'abcd' collate latin1_</span>bin; select upper(@s1),upper(@s2); <span class="hljs-code">+------------+</span>------------+ <span class="hljs-header">| upper(@s1) | upper(@s2) | +------------+------------+</span> <span class="hljs-header">| abcd | ABCD | +------------+------------+</span></code>
二进制串根本没有字符集的概念,所以无从得知哪些字节值对应着大写或者小写字符.如果非要这么做,可以选择先 convert再使用upper lower函数.
3.1.2.2字符集相关的系统变量
<code>这一小节pass 暂时并没有什么用 一切使用默认就好了 </code>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

Detailed explanation of database ACID attributes ACID attributes are a set of rules to ensure the reliability and consistency of database transactions. They define how database systems handle transactions, and ensure data integrity and accuracy even in case of system crashes, power interruptions, or multiple users concurrent access. ACID Attribute Overview Atomicity: A transaction is regarded as an indivisible unit. Any part fails, the entire transaction is rolled back, and the database does not retain any changes. For example, if a bank transfer is deducted from one account but not increased to another, the entire operation is revoked. begintransaction; updateaccountssetbalance=balance-100wh

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

View the MySQL database with the following command: Connect to the server: mysql -u Username -p Password Run SHOW DATABASES; Command to get all existing databases Select database: USE database name; View table: SHOW TABLES; View table structure: DESCRIBE table name; View data: SELECT * FROM table name;
