MySQL basics - multi-table query and transaction management

MySQL basics - multi-table query and transaction management

1. Multi-table query

1.1 Correspondence

  • one to one

​ Mostly used for single-table splitting, put the basic fields of a table in one table, and put other detailed fields in another table to improve operational efficiency

​Add a foreign key to

​ Because it is one-to-one, a unique constraint needs to be set

image-20230522112238330

  • one-to-many

​Create a foreign key on

image-20230522112109287

  • many to many

​Create a third intermediate table. The intermediate table contains at least two foreign keys, which are associated with the primary keys of the two parties.

image-20230522112143302

1.2 Prepare data

student form

create table student(

  id int auto_increment primary key comment '主键ID',
  name varchar(10) comment '姓名',
	
  no varchar(10) comment '学号'
	
) comment '学生表';

insert into student values (null, '黛绮丝', '2000100101'),(null, '谢逊','2000100102'),
                           (null, '殷天正', '2000100103'),(null, '韦一笑', '2000100104');
													 

Class Schedule

create table course(

  id int auto_increment primary key comment '主键ID',
	
  name varchar(10) comment '课程名称'
	
) comment '课程表';

insert into course values (null, 'Java'), (null, 'PHP'), 
                          (null , 'MySQL') ,(null, 'Hadoop');		
										 

Student schedule association table

create table student_course(

  id int auto_increment comment '主键' primary key,
	
  studentid int not null comment '学生ID', 
	
  courseid int not null comment '课程ID',
	
  constraint fk_courseid foreign key (courseid) references course (id),
	
  constraint fk_studentid foreign key (studentid) references student (id)
	
)comment '学生课程中间表';

insert into student_course values (null,1,1),(null,1,2),(null,1,3),
                                  (null,2,2),(null,2,3),(null,3,4);

dept table

-- 创建dept表,并插入数据
create table dept(
  id int auto_increment comment 'ID' primary key,
	
  name varchar(50) not null comment '部门名称'
	
)comment '部门表';

INSERT INTO dept (id, name) VALUES (1, '研发部'), (2, '市场部'),(3, '财务部'),
                                   (4,'销售部'), (5, '总经办'), (6, '人事部');

create emp table

-- 创建emp表,并插入数据
create table emp(

  id int auto_increment comment 'ID' primary key,
	
  name varchar(50) not null comment '姓名',
	
  age int comment '年龄',
	
  job varchar(20) comment '职位',
	
  salary int comment '薪资',
	
  entrydate date comment '入职时间',
	
  managerid int comment '直属领导ID',
	
  dept_id int comment '部门ID'
	
)comment '员工表';

-- 添加外键
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);


INSERT INTO emp (id, name, age, job,salary, entrydate, managerid, dept_id)
VALUES
(1, '金庸', 66, '总裁',20000, '2000-01-01', null,5),
(2, '张无忌', 20, '项目经理',12500, '2005-12-05', 1,1),
(3, '杨逍', 33, '开发', 8400,'2000-11-03', 2,1),
(4, '韦一笑', 48, '开发',11000, '2002-02-05', 2,1),
(5, '常遇春', 43, '开发',10500, '2004-09-07', 3,1),
(6, '小昭', 19, '程序员鼓励师',6600, '2004-10-12', 2,1),
(7, '灭绝', 60, '财务总监',8500, '2002-09-12', 1,3),
(8, '周芷若', 19, '会计',48000, '2006-06-02', 7,3),
(9, '丁敏君', 23, '出纳',5250, '2009-05-13', 7,3),
(10, '赵敏', 20, '市场部总监',12500, '2004-10-12', 1,2),
(11, '鹿杖客', 56, '职员',3750, '2006-10-03', 10,2),
(12, '鹤笔翁', 19, '职员',3750, '2007-05-09', 10,2),
(13, '方东白', 19, '职员',5500, '2009-02-12', 10,2),
(14, '张三丰', 88, '销售总监',14000, '2004-10-12', 1,4),
(15, '俞莲舟', 38, '销售',4600, '2004-10-12', 14,4),
(16, '宋远桥', 40, '销售',4600, '2004-10-12', 14,4),
(17, '陈友谅', 42, null,2000, '2011-10-12', 1,null);

1.3 Overview

Q: Why is the final result of select * from emp, dept 102?

​ emp records 17 records, dept records 6 records, 17*6=102

​ In fact, it is all the combinations of all the records (17) of the employee table emp and all the records (6) of the department table dept. This phenomenon is called Cartesian product . In these combination situations, many records are useless and we need to eliminate them. Cartesian Product

image-20230522155734067

Cartesian Product

All cases of left set and right set

image-20230522160308623

How to eliminate Cartesian product?

​ Just add conditions when connecting multiple tables

select * from emp , dept where emp.dept_id = dept.id;

image-20230522160614331

1.4 Inner join

The inner join query is the data in the intersection of two tables. (that is, the data in the green part)

image-20230522161208113

There are two types of inner joins: implicit inner joins and explicit inner joins

  • implicit inner join
SELECT 字段列表 
FROM1 ,2
WHERE 条件 ... ;
  • explicit inner join

    SELECT 字段列表 
    FROM1 [ INNER ] 
    JOIN2 ON 连接条件 ... ;
    

the case

  • Query the name of each employee, and the name of the associated department (explicit inner connection implementation)
SELECT emp.name '员工姓名', dept.name '部门名称'
FROM emp
 join dept on emp.dept_id = dept.id
 
  • Query the name of each employee, and the name of the associated department (implicit inner connection implementation)
select emp.name , dept.name
from emp , dept 
where emp.dept_id = dept.id ;

1.5 Outer join

There are two types of outer joins: left outer join and right outer join.

image-20230522161208113

  • left outer join
SELECT 字段列表
FROM1 
LEFT [ OUTER ] JOIN2 ON 条件 ... ;

The left outer join is equivalent to querying all the data in table 1 (left table), and of course also includes the data in the intersection of table 1 and table 2.

  • right outer join
SELECT 字段列表 
FROM1
RIGHT [ OUTER ] JOIN2 ON 条件 ... ;

​ The right outer join is equivalent to querying all the data in table 2 (right table), and of course also including the data in the intersection of table 1 and table 2

the case

  • Query all the data in the emp table, and the corresponding department information
select e.*, d.name 
from emp e 
left outer join dept d on e.dept_id = d.id;
  • Query all the data in the dept table, and the corresponding employee information (right outer join)
select d.*, e.* 
from emp e 
right outer join dept d on e.dept_id = d.id;

Precautions

​ Left outer join and right outer join can be replaced with each other, you only need to adjust the order of the table structure in SQL when connecting queries. In our daily development and use, we prefer the left outer connection

1.6 Self-join

​ is to connect a table to query multiple times.

SELECT 字段列表 
FROM 表A 别名A 
JOIN 表A 别名B ON 条件 ... ;

​Notes

​ In the self-join query, the table must be aliased, otherwise we do not know the specified conditions, the returned fields, and which table field it is.

the case

  • Query the names of employees and their leaders
select a.name , b.name

from emp a , emp b 

where a.managerid = b.id;
  • Query the name emp of all employees emp and their leaders. If the employee has no leader, it also needs to be queried
select a.name '员工', b.name '领导' 
from emp a 
left join emp b on a.managerid =b.id;

1.7 joint query union

​ For union queries, the results of multiple queries are combined to form a new query result set.

  • For the joint query, the number of columns and field types of multiple tables must be consistent .
  • union all will directly merge all the data together, and union will deduplicate the merged data .
SELECT 字段列表 FROM 表A ...
UNION [ ALL ]
SELECT 字段列表 FROM 表B ....;

the case

  • Query all employees whose salary is less than 5000 and employees whose age is greater than 50

​ Use union, because union not only merges, but also deduplicates the data. There may be some people whose salary is less than 5000 and whose age is greater than 50

select * from emp where salary < 5000
union
select * from emp where age > 50;

1.8 Subqueries

​ Nested SELECT statements in SQL statements are called nested queries, also known as subqueries.

grammar

SELECT * 
FROM t1 
WHERE column1 = ( SELECT column1 FROM t2 ); 

The statement outside the subquery can be any one of INSERT / UPDATE / DELETE / SELECT.

Classification

  • Scalar subquery (subquery result is a single value)
  • Column subquery (the result of the subquery is a column)
  • Row subquery (the result of the subquery is one row)
  • Table subquery (the result of the subquery is multiple rows and multiple columns)

1.8.1 Scalar Subqueries

The result returned by a subquery is a single value (number, string, date, etc.). In its simplest form, such a subquery is called a scalar subquery .

Commonly used operators : = <> > >= < <=

  • Query all employee information of "Sales Department"
select * 
from emp 
where dept_id = (
                  select id 
				  from dept 
				  where name = '销售部'
				 );
  • Query employee information after joining "Fang Dongbai"
select * 
from emp 
where entrydate > ( 
                     select entrydate 
					 from emp 
					 where name = '方东白'
				  );

1.8.2 Column query

The result returned by a subquery is a column (can be multiple rows) , and this subquery is called a column subquery .

common operators

operator describe
IN Within the specified collection range, choose one more
NOT IN is not within the specified collection range
ANY In the list returned by the subquery, any one can satisfy
SOME Equivalent to ANY, ANY can be used wherever SOME is used
ALL All values ​​in the list returned by the subquery must satisfy

the case

  • Query all employee information of "Sales Department" and "Marketing Department"
select * 

from emp

where dept_id in (
                   SELECT id

                   from dept

                   where name = '销售部' or name = '市场部'
                )

  • Query information about employees whose salary is higher than that of everyone in the finance department
select * 
from emp
where salary > (
                select MAX(salary)
                from emp
                where dept_id = (
                       select id
                       from dept
                       where name ='财务部'
								     	)
               )

or

select * 
from emp 
where salary > all (
                    select salary 
					from emp 
					where dept_id =(
								 select id 
								 from dept 
								 where name = '财务部'
                                    )
					);
  • Query information about employees whose salary is higher than that of anyone in the R&D department
select * 
from  emp
where salary > any (
                   select salary
                   from emp
                   where dept_id = '1'
                 );

1.8.3 Row subqueries

The result returned by a subquery is a row (can be multiple columns), this subquery is called a row subquery

Common operators : = , <> , IN , NOT IN

  • Query the same employee information as "Zhang Wuji"'s salary and direct leadership;
SELECT *
from emp
where (salary , managerid) = (
                    select salary , managerid
                    from emp
                    where name = '张无忌'
                              )

1.8.4 Table subqueries

The result returned by a subquery is multiple rows and multiple columns. This kind of subquery is called a table subquery.

Commonly used operators : IN

  • Query employee information with the same position and salary as "Luzhangke" and "Song Yuanqiao"
select *
from emp
where (salary ,managerid) in (
                     select salary ,managerid
                     from emp
                     where name ='鹿杖客' or name = '宋远桥'
										 )

Two, affairs

2.1 Introduction

​A transaction is a collection of a group of operations, which is an indivisible unit of work. A transaction will submit or revoke an operation request to the system together with all operations as a whole, that is, these operations either succeed at the same time or fail at the same time

​ Start the transaction before the business logic is executed, and submit the transaction after the execution is completed. If an error is reported during execution, the transaction is rolled back and the data is restored to the state before the transaction started.

image-20230522181739056

Note :

​ The default MySQL transaction is automatically committed, that is, when a DML statement is executed, MySQL will immediately and implicitly commit the transaction.

2.2 Operation Demonstration

data preparation

drop table if exists account;

create table account(
  id int primary key AUTO_INCREMENT comment 'ID',
	
  name varchar(10) comment '姓名',
	
  money double(10,2) comment '余额'
	
) comment '账户表';

insert into account(name, money) VALUES ('张三',2000), ('李四',2000);

normal circumstances

-- 1. 查询张三余额
select * from account where name = '张三';

-- 2. 张三的余额减少1000
update account set money = money - 1000 where name = '张三';

-- 3. 李四的余额增加1000
update account set money = money + 1000 where name = '李四';

image-20230522182600626

​ But suppose we made a mistake after the second step, and Zhang San’s balance decreased by 1000, but due to an exception being thrown, the third step was not executed or the third step was not executed, so 1000 was lost out of thin air

For now, three SQL statements are three transactions, and they will be submitted automatically

In order to avoid this situation, we need to control these operations within a transaction scope

2.3 Control transactions

2.3.1 Control Transaction 1

  • View/set transaction submission method

If it is '1', it is automatically submitted, if it is '0', it is manually submitted

SELECT @@autocommit ;
SET @@autocommit = 0 ;
  • commit transaction
COMMIT;
  • rollback transaction
ROLLBACK;

Note :

​ In the above method, we have modified the automatic commit behavior of the transaction, and changed the default automatic commit to manual commit. At this time, none of the DML statements we execute will be committed, and we need to manually execute commit to commit .

2.3.2 Control Transaction II

The transaction submission method will not be modified, and the transaction will be opened directly

  • open transaction
START TRANSACTIONBEGIN ;
  • commit transaction
COMMIT;
  • rollback transaction
ROLLBACK;
-- 开启事务
start transaction;

-- 1. 查询张三余额
SELECT * from account where name ='张三';

-- 2. 张三的余额减少1000
update account set money = (money - 1000) where name = '张三';

-- 3. 李四的余额增加1000
update account set money = money + 1000 where name = '李四';

-- 如果正常执行完毕, 则提交事务
commit;
-- 如果执行过程中报错, 则回滚事务
-- rollback;

2.4 ACID, the four major characteristics of transactions

  • Atomicity : A transaction is an indivisible minimum unit of operation, either all succeed or all fail.

  • Consistency : When the transaction is completed, all data must be kept in a consistent state

​ For example, if Zhang San and Li Si transfer money, whether the transfer is successful or the transfer fails, the sum of the two people's amount is constant

  • Isolation : The isolation mechanism provided by the database system ensures that transactions run in an independent environment that is not affected by external concurrent operations.

For example, A transaction and B transaction operate the database at the same time, A transaction will not affect the execution of concurrent B transaction during operation, and similarly, the execution of B transaction will not affect the execution of A transaction

  • Durability : Once a transaction is committed or rolled back, its changes to the data in the database are permanent

2.5 Concurrent transaction issues

  • dirty read

​A transaction reads data that has not yet been committed by another transaction

​ Transaction A changes the data with ID 1, but does not submit it. At this time, transaction B also reads the data with ID 1. Coincidentally, it reads the modified data of transaction A.

​ Obviously this is incorrect, because transaction A did not submit data, and the content read by transaction B should still be the content before modification

image-20230523131620624

  • Unrepeatable

​A transaction reads the same record successively, but the data read twice is different, which is called non-repeatable read

​ As shown below, transaction A reads the data with ID 1. After the reading is completed, transaction B changes the data with ID 1. After the change is completed, transaction A reads the data with ID 1 again. It is found that two reads The fetched data content is different

image-20230523131932843

  • Phantom reading

​When a

​ Transaction A first queries the database for the data with ID 1, and finds that there is no

At this time, transaction B inserts a piece of data with ID 1 into the database

​ Transaction A inserts the data with ID 1 into the database and reports an error, because the ID is 1, it cannot be inserted again

​ Transaction A reads the data with ID 1 from the database, but finds that the data cannot be found (the data submitted by transaction B cannot be read, because we have solved the problem of "non-repeatable read").

​ After executing the insertion, it is obviously there, but it cannot be found. This is called hallucination, so we call it phantom reading

image-20230523132438470

2.6 Transaction isolation level

In order to solve the problems caused by concurrent transactions

The higher the transaction isolation level, the more secure the data, but the lower the performance . From top to bottom, the transaction isolation level increases

isolation level dirty read non-repeatable read Phantom reading
Read uncommitted read uncommitted
**Read committed ** Read committed ×
Repeatable Read (default) repeatable read × ×
SerializableSerialization _ × × ×

Oracle's default isolation level is Read committed and read committed

  • View transaction isolation level
SELECT @@TRANSACTION_ISOLATION;
  • Set transaction isolation level
    • SESSION sets the session level, which only means that it is valid for the current client window
    • GLOBAL is valid for all client session windows
SET [ SESSION | GLOBAL ] 
TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE }

ISOLATION means isolation

  • Set the isolation level to "repeatable read"
SET SESSION TRANSACTION ISOLATION LEVEL  REPEATABLE READ 

isolation level dirty read non-repeatable read Phantom reading
Read uncommitted read uncommitted
**Read committed ** Read committed ×
Repeatable Read (default) repeatable read × ×
SerializableSerialization _ × × ×

Oracle's default isolation level is Read committed and read committed

  • View transaction isolation level
SELECT @@TRANSACTION_ISOLATION;
  • Set transaction isolation level
    • SESSION sets the session level, which only means that it is valid for the current client window
    • GLOBAL is valid for all client session windows
SET [ SESSION | GLOBAL ] 
TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE }

ISOLATION means isolation

  • Set the isolation level to "repeatable read"
SET SESSION TRANSACTION ISOLATION LEVEL  REPEATABLE READ 

Guess you like

Origin blog.csdn.net/weixin_51351637/article/details/130863570