当前位置:网站首页>MySQL series tutorial (I) getting to know MySQL

MySQL series tutorial (I) getting to know MySQL

2022-06-24 05:50:00 ruochen

database

Database, It's organized according to the data structure 、 Warehouse for storing and managing data

function

  • Persistent storage and management ( increase Delete to update ) Large amount of data
  • Efficiently query and extract data that meet the conditions , Fast data access
  • Constraining the type of data stored
  • Support remote data sharing and permission management

Database management system (DBMS)

  • Operation and management 、 Large scale software for building and using databases , Ensure the security and integrity of database data
  • Can pass DBMS Accessing data in the database , Maintenance of database
  • Provide data definition language DDL And data manipulation language DML, adopt DNMS Interface to read and write data , Realize the addition of data in the database 、 Delete and other operations
  • Allow multiple users to set up at different times , Modify and access the database
     The data in the file ,  Can pass Excel and wps Open view , Add and delete data 

DBMS classification

  • Relational type
MySQL( Is the most popular open source database management system )、Microsoft SQL Server、Oracle( charge )、pgSQL
  • Non relational
MongoDb、redis、Cloudant、HBase

Relational database

characteristic

1·  Data is presented in tabular form , Each table has its own name ( Unique in the same database ), Tables are organized as one-to-one correspondence between rows and columns 
2.  Name of various data records 
3.  Each column is the data field corresponding to the record name 
4·  Many rows and columns form a table 
5. Several tables form a relational database (database),  Each database has its own name ( only )

The term

  • data : data
  • That's ok : The data in the table is stored by row , Each record saved is stored in its own line ( Data records )
  • Column : A column of ( data ), The same column has data elements of the same type
  • Data sheet : A table is a matrix of data , A table in a database looks like a simple spreadsheet
  • database : Set of tables db
  • Primary key : This column uniquely identifies each row in the table ( Or this set of columns ) Called the primary key , Used to represent a specific line , Primary key column not allowed NULL value
 There is no primary key , It's difficult to update or delete specific rows in a table , Because there is no safe way to ensure that only the relevant lines are involved . Although primary keys are not always required , But most database designers should ensure that each table they create has a primary key , To facilitate data manipulation and management in the future .
  • Indexes : Use indexes to quickly access data in a data table

MySQL database

  • MySQL It's open source and free , There is no extra charge
  • MySQL Support large databases , Support 5000 Data warehouse of ten thousand records ,32 Bit system table files can support 4GB,64 The largest table file supported by bit system is 8TB.
  • MySQL standards-of-use SQL Data language form
  • MySQL Can run on multiple systems , And support multiple languages . These programming languages include C、C++、Python, Java,Perl. PHP, Eiffel. Ruby and Tcl etc.

advantage

  1. Simple and easy to use
MySQL  It is a high-performance and relatively simple database system , Compared with the setup and management of some larger systems , It's less complicated 
  1. Price
 It is free for most individual users , Anyone can download, install and use it on the official website 
  1. small
 The database distribution is only 21M, The installation is only 51M
  1. Support query language
 You can use SQL ( Structured query language ),SQL Is a language used by all modern database systems 
  1. performance
 No limit on the number of users , Multiple clients can use the same database at the same time .MySQL Very fast . The developer claims MySQL Probably the fastest database available so far 
  1. Connectivity and security
MySQL It's completely networked , Can be accessed anywhere , You can share the database with anyone anywhere , You can control who can't see your data 

shortcoming

  • In order to maintain consistency, the huge cost is its poor read-write performance
  • Fixed watch structure
  • High concurrent read and write requirements
  • Efficient reading and writing of massive data

The server handles client requests

In fact, no matter which way the client process and server process communicate , The final effect is : The client process sends a text message to the server process (MySQL sentence ), The server process sends a piece of text to the client process after processing ( Processing results ). What does the server process do with the request sent by the client process , To produce the final processing result ? The client can send various requests for addition, deletion, modification and query to the server , Let's take a more complex query request as an example to draw a diagram to show the general process :

image_1c8d26fmg1af0ms81cpc7gm8lv39.png-97.9kB

Although query caching can sometimes improve system performance , But it also has to incur some overhead due to maintaining this cache , For example, we need to search in the query cache every time , Query cache needs to be updated after query request processing , Maintain the memory area corresponding to the query cache . from MySQL 5.7.20 Start , Query caching is not recommended , And in MySQL 8.0 Delete in .

Storage engine

MySQL The server encapsulates the data storage and extraction operations into a file called Storage engine In the module of . We know surface It consists of line by line records , But it's just a logical concept , How to represent records physically , How to read data from a table , How to write data to specific physical memory , This is all Storage engine Responsible things . In order to achieve different functions ,MySQL Offers a wide variety of Storage engine , Different Storage engine The specific storage structure of managed tables may be different , Access algorithms may also be different .

The storage engine used to be called Watch processor , Its function is to receive instructions from the upper layer , Then extract or write the data in the table .

For management convenience , People put Connection management The query cache Syntax parsing Query optimization These functions that do not involve real data storage are divided into MySQL server The function of , The functions of real data access are divided into Storage engine The function of . Different storage engines up MySQL server Layer provides a unified calling interface ( Storage engine API), Contains dozens of underlying functions , image " Read the first item of index "、" Read index next "、" insert record " wait .

So in MySQL server After query optimization , You only need to call the API, Just get the data and return it to the client .

MySQL Support for a wide variety of storage engines :

Storage engine

describe

ARCHIVE

For data archiving ( Rows cannot be modified after being inserted )

BLACKHOLE

Discard write operation , The read operation will return empty content

CSV

When storing data , Separate data items with commas

FEDERATED

Used to access remote tables

InnoDB

Transaction storage engine with foreign key support

MEMORY

Table in memory

MERGE

Used to manage multiple MyISAM A collection of tables made up of tables

MyISAM

The main non transactional storage engine

NDB

MySQL Cluster specific storage engine

Some operations about the storage engine

View the storage engines supported by the current server program

We can use the following command to view the storage engines supported by the current server program :

SHOW ENGINES;

Set the storage engine for the table

We said that before , The storage engine is responsible for extracting and writing the data in the table , We can set different storage engines for different tables , In other words, different tables can have different physical storage structures , Different extraction and writing methods .

Specify the storage engine when creating the table

Our previous statements to create tables did not specify the storage engine of the table , Then the default storage engine will be used InnoDB( Of course, the default storage engine can also be modified , We will talk about how to change it in the following chapters ). If we want to explicitly specify the storage engine of the table , It can be written like this :

CREATE TABLE  Table name (
     Create table statement ;
) ENGINE =  Storage engine name ;

For example, we want to create a storage engine for MyISAM Your watch can say this :

mysql> CREATE TABLE engine_demo_table(
    ->     i int
    -> ) ENGINE = MyISAM;
Query OK, 0 rows affected (0.02 sec)

mysql>

Modify the storage engine of the table

If the table has been built , We can also use the following statement to modify the storage engine of the table :

ALTER TABLE  Table name  ENGINE =  Storage engine name ;

For example, let's modify engine_demo_table Table storage engine :

mysql> ALTER TABLE engine_demo_table ENGINE = InnoDB;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>

Now let's check again engine_demo_table The table structure :

mysql> SHOW CREATE TABLE engine_demo_table\G
*************************** 1. row ***************************
       Table: engine_demo_table
Create Table: CREATE TABLE `engine_demo_table` (
  `i` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

mysql>

You can see that the storage engine of this table has been changed to InnoDB 了 .

Learn about character sets and garbled code

Character set Introduction

We know that only binary data can be stored in a computer , How to store strings ? Of course, it is to establish the mapping relationship between characters and binary data , To establish this relationship, we should at least find out two things :

  1. Which characters do you want to map into binary data ?

That is, clearly define the character range .

  1. How to map ?

The process of mapping a character to binary data is also called code , The process of mapping a binary data to a character is called decode .

People Abstract A Character set To describe the coding rules of a certain character range

Let's take a look at some common character sets :

  • ASCII Character set

Collects 128 Characters , Including Spaces 、 Punctuation 、 Numbers 、 Upper and lower case letters and some invisible characters . Because it's only 128 Characters , So you can use 1 Two bytes to encode , Let's look at the encoding of some characters :

'L' -> 01001100( Hexadecimal :0x4C, Decimal system :76)

'M' -> 01001101( Hexadecimal :0x4D, Decimal system :77)

  • ISO 8859-1 Character set

Collects 256 Characters , Is in ASCII On the basis of the character set, it has expanded 128 Four common characters in Western Europe ( Including the letters of Germany and France ), You can also use 1 Two bytes to encode . This character set also has an alias latin1.

  • GB2312 Character set

It includes Chinese characters and Latin letters 、 The Greek letter 、 Japanese hiragana and katakana letters 、 Russian Cyrillic alphabet . It contains Chinese characters 6763 individual , Other characters 682 individual . At the same time, this character set is compatible with ASCII Character set , So it seems strange in the way of coding :

  • If the character is in ASCII Character set , Then 1 Byte encoding .
  • Otherwise the 2 Byte encoding .

The number of bytes required to represent a character may be encoded in different ways, which is called Variable length encoding . For example, string ' Love u', among ' Love ' Need to use 2 Bytes for encoding , The encoded hexadecimal representation is 0xCED2,'u' Need to use 1 Bytes for encoding , The encoded hexadecimal representation is 0x75, So put together is 0xCED275.

Tips: : How can we tell whether a byte represents a single character or a part of a character ? Don't forget ASCII The character set contains only 128 Characters , Use 0~127 Can represent all characters , So if a byte is in 0~127 Within , It means that a byte represents a single character , Otherwise, two bytes represent a single character .

  • GBK Character set

GBK The character set is only in the range of included characters GB2312 The character set is extended , The coding method is compatible GB2312.

  • utf8 Character set

All the characters you can think of on earth , And it's expanding . This character set is compatible with ASCII Character set , Using variable length coding , Encoding a character requires the use of 1~4 Bytes , Like this :

'L' -> 01001100( Hexadecimal :0x4C)

' ah ' -> 111001011001010110001010( Hexadecimal :0xE5958A)

Tips: : In fact, to be exact ,utf8 It's just Unicode A coding scheme for character sets ,Unicode The character set can be utf8、utf16、utf32 These coding schemes ,utf8 Use 1~4 One byte encodes one character ,utf16 Use 2 Or 4 One byte encodes one character ,utf32 Use 4 One byte encodes one character . More detailed Unicode Knowledge of and its coding scheme is not the focus of this book , Let's check the Internet ~ MySQL The concept of character set and encoding scheme is not distinguished in , So when you nag later, put utf8、utf16、utf32 Are treated as a character set .

For the same character , Different character sets may also have different encoding methods . For example, for Chinese characters ' I ' Come on ,ASCII This character is not included in the character set at all ,utf8 and gb2312 Character set for Chinese characters I The encoding method of is as follows :

utf8 code :111001101000100010010001 (3 Bytes , Hexadecimal means :0xE68891)
gb2312 code :1100111011010010 (2 Bytes , Hexadecimal means :0xCED2)

MySQL Medium utf8 and utf8mb4

We said above utf8 The character set indicates that a character needs to use 1~4 Bytes , But some of the characters we often use 1~3 It's just a byte . And in the MySQL The character set in represents the maximum byte length of a character, which will affect the storage and performance of the system in some ways , So design MySQL My uncle secretly defined two concepts :

  • utf8mb3: Castrated utf8 Character set , Use only 1~3 Bytes represent characters .
  • utf8mb4: authentic utf8 Character set , Use 1~4 Bytes represent characters .

There is one point that requires your great attention , stay MySQL in utf8 yes utf8mb3 Another name for , So after that MySQL I mentioned utf8 It means using 1~3 Bytes to represent a character , If you use 4 The case of byte encoding a character , For example, store some emoji Expression or something , Please use utf8mb4.

View of character set

MySQL Support many character sets , View the current MySQL The following statement can be used for the character sets supported in :

原网站

版权声明
本文为[ruochen]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/08/20210803140109200j.html

猜你喜欢