MySQL (1) basic architecture, SQL statement operation, try

MySQL Series Articles

MySQL (1) Basic structure, SQL statement operation, try
MySQL (2) Index principle and optimization
MySQL (3) SQL optimization, Buffer pool, Change buffer
MySQL (4) Transaction principle and analysis
MySQL (5) Caching strategy
MySQL (6) Three paradigms of master-slave replication database



foreword

MySQL is a relational database. Databases are used to store data.


What does relational mean?
A relational database is similar to an excel table, and each unit in each row and column can find associated data in the table.
The entire library is like a web of relationships.
For example:
insert image description here


So what is a non-relational database?
Non-relational databases are similar to redis, which use key-value storage. Similar to a hash table data structure. It is conceivable that there is no association between each stored data in the key-value store.
For example:

tony:35
aries:18

1. MySQL network structure

MySQL is divided into server and client. After installing MySQL, we need to start the server and then connect with the client. Of course, multiple clients can connect to a MySQL server. Therefore, the connection between the client and the server involves network communication. The MySQL network architecture usually refers to the network architecture implemented by the server, because to connect with multiple clients, all concurrency scenarios need to be considered.
The main processing method of Mysql network is IO multiplexing select + blocking io; select only listens to listenfd, and does not care about the reading and writing of connection threads. select is cross-platform, mysql can run under Linux and windows;
while redis (using epoll) can only run under Linux, it is replaced by select in windows, and windows has no fork sub-thread, so the function is not complete.

2. The steps experienced by a SQL statement

The steps that a SQL statement goes through on the server side are still relatively complicated:
first go through the connector (establish, manage connections, verify user information) and then query the cache, find a direct hit, and continue to run if not found, then the SQL statement is analyzed by the analyzer Statement analysis, syntax analysis, and syntax tree generation; the optimal execution step is selected by the optimizer; data is obtained from the storage engine through the executor according to the execution plan, and returned to the client.
insert image description here

Three, MySQL operation

increase

insert

INSERT INTO `table_name`(`field1`, `field2`, ...,`fieldn`) VALUES (value1, value2, ..., valuen);
//例如
INSERT INTO test_db (id, name, age) VALUES ("3", "lihua", 27);

delete

Three ways to delete data: drop, truncate, and delete speeds decrease in turn

DROP TABLE `table_name`;//删除整张表,包括索引,约束,触发器等(不能回滚)
TRUNCATE TABLE `table_name`;//删除表数据,以以页为单位删除;其他保留(不能回滚)
DELETE TABLE `table_name`;//删除部分或全部数据,逐行删除,其他保留(条件删除)可以回滚

check

SELECT field1, field2,...fieldN FROM table_name[WHERE Clause]

change

UPDATE table_name SET field1=new_value1,field2=new_value2 [, fieldn=new_valuen]

Advanced Search

Advanced query mainly understands grouping query and aggregation query

Group query

That is to add conditional judgment:
1.where condition
2.group by column having condition

-- 分组加group_concat
| id | name   | gender | age |
|----|--------|--------|-----|
| 1  | Alice  | Female | 20  |
| 2  | Bob    | Male   | 22  |
| 3  | Charlie| Male   | 21  |
| 4  | Dave   | Male   | 23  |
| 5  | Eve    | Female | 19  |
SELECT `gender`, group_concat(`age`) FROM `student` GROUP BY `gender`;//以gender分组,将同组的age合并起来组成一个年龄字符串
| gender | group_concat(age) |
|--------|---------------------|
| Female | 20,19                |
| Male   | 22,21,23             |

-- 分组加条件(having的条件可以用select中本条命令查到的,而where做不到)
SELECT `gender`, count(*)  FROM as num `student` where num > 6;

————————————————

aggregation query

insert image description here

SELECT sum(`num`) FROM `score`;

Multi-table joint query

Divided into inline query and external query
Inline : inner join, only take records with corresponding relationship between two tables

//从两个名为"course"和"teacher"的表中获取课程ID和对应的教师ID。
假设"course"表中有以下数据:
| cid | name        | teacher_id |
|-----|-------------|-----------|
| 1   | Calculus    | 101       |
| 2   | Physics     | 102       |
| 3   | Chemistry   | 103       |
| 4   | Computer Science | 105 |
| 5   | Biology     | 104       |
"teacher"表中有以下数据:
| tid | name      |
|-----|-----------|
| 101 | Smith    |
| 102 | Johnson  |
| 103 | Lee      |
| 104 | Davis    |
SELECT cid FROM `course` INNER JOIN `teacher` ON course.teacher_id =teacher.tid;
| cid |
|-----|
| 1   |
| 2   |
| 3   |
| 5   |

Outer join : Divided into left join and right join; on the basis of inner join, records with no corresponding relationship between left table and right table are kept

假设"course"表中有以下数据:

| cid | name        | teacher_id |
|-----|-------------|-----------|
| 1   | Calculus    | 101       |
| 2   | Physics     | 102       |
| 3   | Chemistry   | 103       |
| 4   | Computer Science | 101 |
| 5   | Biology     | 104       |

"teacher"表中有以下数据:

| tid | name      |
|-----|-----------|
| 101 | Smith    |
| 102 | Johnson  |
| 103 | Lee      |
SELECT course.cid teacher.name FROM `course` LEFT JOIN `teacher` ON course.teacher_id =teacher.tid;
| cid | name    |
|-----|---------|
| 1   | Smith   |
| 2   | Johnson |
| 3   | Lee     |
| 4   | Smith   |
| 5   | NULL    |
SELECT course.cid teacher.name FROM `course` RIGHT JOIN `teacher` ON course.teacher_id =teacher.tid;
| cid | name    |
|-----|---------|
| 1   | Smith   |
| 4   | Smith   |
| 2   | Johnson |
| 3	  | Lee     |

4. View

A view ( view ) is a virtual table, a logical table that does not contain data itself. Its content is defined by the query.

Views only do select queries, and do not add, delete, or modify (although it can be done, it is generally not used, and there are many restrictions). In the work project: For example, a recharge table, I only give you a view, but not a table, so you can't modify this core resource. It can only be checked, not changed.

effect:

  • Reusable, reducing repetitive statement writing; similar to the role of functions in programs; refactoring tool:
    (If due to some needs, user needs to be split into table usera and table userb for query; if the application uses sql statement: select * from user, it will prompt that the table does not exist; if you do not directly dismantle the table at this time, but create a view create view user as select a.name,a.age,b.sex from usera as a, userb as b where a.name= b.name; you only need to change the database structure, not the application program;)
    The logic is clearer, the query details are shielded, and the data return is focused;
  • Access control, some tables are shielded from the user, but the user can operate the table through the view;

Guess you like

Origin blog.csdn.net/weixin_44477424/article/details/131736109