The fourth day of database learning

The fourth day of database learning

Today's content

1 Table join query
2 Subquery
3 Transaction
4 Isolation level of transaction
5 DCL

Table join query

1 What is a multi-table query?
For example, the data columns we want to query exist in different tables, then we need to query multiple tables at the same time, this time we need to use multi-table query
2 multi-table query classification
(1) inner join:
* implicit Inner join
* Explicit inner join
(2) Outer join:
*Left outer join
*Right outer join
3 Cartesian product phenomenon
If we query two tables at the same time without any processing, all combinations of all data in the two tables will appear Happening.
Insert picture description here
4 How to deal with the Cartesian product phenomenon?
We found that not all the data in the table in the above figure is useful, only the data whose dept_id in the employee table is equal to the id in the department table is useful,
so you can write and add conditions like this: select emp.name, dept. namefrom emp, dept where emp. dept_id= dept id.;

Internal connection

Use the records of the left table to match the records of the right table. If the conditions are met, it will be displayed as table. Foreign key = primary table. Primary key

Implicit inner join

You can’t see the join key. Use where to specify the
SELECT field name FROM left table and right table WHERE conditions
such as: select * from emp, dept where emp. dept_id= dept id.;
Insert picture description here
Here you can see the data in the employee table and the department table The data will not be displayed until it matches.

Explicit inner join

Use inner join on statement, you can omit inner
SELECT field name FROM left table [INNER] JOIN right table ON conditions
such as: select * from emp e inner join dept d on e. dept_id= d id.;
Insert picture description here

Summarize the operation steps of internal connection

(1) Determine which tables to query?
(2) Determine the condition of the connection
(3) Determine the condition of the query
(4) Determine the field of the query

Left outer join

When we use the table on the left to match the table on the right, the display that meets the conditions is displayed, and the display that does not meet the conditions is displayed as null, which means that all the contents of the left table and the intersection of the left and right tables are displayed. In other words, priority should be given to the left table , And then consider the table on the right, even if there is data that does not meet the conditions of the table on the right, it will be added to the table, and null is displayed where it does not.
For example, we added a sales department to the above department table, but no employee belongs to this department, but we must ensure that all departments are displayed:
select * from dept d left join emp e on d. id= e dept_id.;
Insert picture description here

Right outer join

The same reason as the left outer join, in fact, just master one. In the work, the left outer join is often used, and the table of the right outer join is moved to the left, and the left outer join is used, the same.
Syntax: SELECT field name FROM left table RIGHT [OUTER] JOIN right table ON condition

Subquery

concept:

1 Use the result of one query as the condition of another query.
2 There are query nesting, and the inner query is called a subquery.
3 The subquery needs to use parentheses. For
example, we need to query which employees are in the development department.
If we use ordinary methods:
select id from dept where name='Development department'; here to find out the id of the development department
select name from emp where dept_id=1; here to find out the employees of the development department,
but using a subquery can be done in one step:
select name from emp where dept_id =(select id from dept where name='Development Department');

Three results of subquery

1 The result of the subquery is a single row and single column
Insert picture description here

2 The result is multiple rows and single column
Insert picture description here
3 The result is multiple rows and multiple columns
Insert picture description here

There are different ways to use the three results:
(1) Single row and single column: The result is used as data.
As the example above: select name from emp where dept_id=(select id from dept where name='Development Department');
(2 ) Multiple rows and single column: The result set is similar to an array, and the parent query uses the IN operator. For
example, query the names of employees whose salary is greater than 5000 and which department they come from
-first query the department id of employees whose
salary is greater than 5000 select dept_id from emp where salary > 5000; -query
the name of the department in these department ids
select name from dept where id in (select dept_id from emp where salary> 5000);
(3) Multiple rows and multiple columns: As long as the subquery result has multiple columns, it must be in The following FROM is used as a table, here as a virtual table, so an alias is required, otherwise it cannot be accessed. For
example: query the information of employees who joined after 2011, including department name
-query the information of employees who joined after 2011, including department Name
-Query employees who joined after 2011-1-1 in the employee table
select * from emp where join_date >='2011-1-1';
-Query all department information, combined with the information in the virtual table above, find Dept_id where all department ids are equal to
select * from dept d, (select * from emp where join_date >=‘2011-1-1’) e where
d.id= e.dept_id ;

Summary of subqueries

(1) As long as the subquery result is a single column, it will be used as a condition after WHERE
(2) As long as the subquery result is multiple columns, it will be used as a table after FROM for secondary query

Affairs

Here is an application scenario. If a person goes to the bank to transfer money, but after the money is deducted, there is a problem. If the transaction is not used at this time, then the action of adding money to another account will not be executed, and the money will be deducted!

concept

Transaction execution is a whole, and all SQL statements must be executed successfully. If one of the SQL statements is abnormal, all
SQL statements must be rolled back, and the entire business execution fails.

Commit the transaction manually

Syntax of manually committing the transaction
Insert picture description here
Execution of the
Insert picture description here
transaction After opening the transaction, you can start to write any sql statement, and then judge the result, if successful, write commit to commit, if not successful, write rollback to roll back

Auto commit transaction

MySQL default each DML (CRUD) statements are a separate transaction, each statement will automatically open a transaction, the statement is finished
automatically commit the transaction, MySQL default start automatically submit the transaction
Insert picture description here
to see whether to open MySQL actually commit
Insert picture description here
@@ represents the global Variable, 1 means on, 0 means off,
cancel auto commit transaction
Insert picture description here

Rollback point

What is the rollback point

After some successful operations are completed, subsequent operations may succeed or fail, but regardless of success or failure, the previous operations have been
successful, and a rollback point can be set at the current successful position. It can be used for subsequent failed operations to return to this position instead of returning to all operations. This point is called
the rollback point.

Operation statement for rollback point

Insert picture description here

Specific operation

  1. Restore data to 1000
  2. Open transaction
  3. Let Zhang San’s account deduct 3 times, 10 yuan each time
  4. Set the rollback point: savepoint three_times;
  5. Let Zhang San’s account deduct 4 times, 10 yuan each time
  6. Back to the rollback point: rollback to three_times;
  7. Analyze the execution process
    Summary: Setting the rollback point allows us to return to the rollback point when it fails, rather than when the transaction is started.

Isolation level of the transaction (understand)

Four characteristics of transaction ACID (need to remember)

Insert picture description here

Problems caused by concurrency Insert picture description here

MySQL database has four isolation levels

Insert picture description here
The higher the isolation level, the worse the performance and the higher the security

MySQL transaction isolation level related commands

Query the isolation level select @@tx_isolation;
set the isolation level set global transaction isolation level level string;
such as: set global transaction isolation level read uncommitted;

DCL

We now use the root user by default, the super administrator, who has all the permissions. However, the database server in a company
may run the databases of many projects at the same time. Therefore, we should be able to create different users according to different projects and assign different permissions to
manage and maintain the database.
Note: mysqld is the main program of MySQL, the server side. mysql is a command line tool and client for MySQL.
Insert picture description here

Create user

Syntax:
CREATE USER'user name'@'host name' IDENTIFIED BY'password';
Keyword description:
Insert picture description here
specific operation:
create user1 user, you can only log in to the mysql server on localhost, the password is 123
create user'user1'@' localhost' identified by '123';
Create user2 users can log in to the mysql server on any computer, the password is 123
create user'user2'@'%' identified by '123';
== Note, if you want to view the user's information, you can Open the user table in the mysql library to view

Authorize users

Syntax:
GRANT permission 1, permission 2... ON database name. table name TO'user name'@'host name';
keyword description:
Insert picture description here
specific operation:
assign user1 user authority to the database operation of test: create table, modify table , Insert record, update record, query
grant create, alter, insert, update, select on test.* to'user1'@'localhost';
The user name and host name must be the same as those created above, with single quotes.
Assign all permissions to the user2 user, for all tables in all databases
Insert picture description here

Revoke authorization

Syntax:
REVOKE authority 1, authority 2... ON database. Table name revoke all on test.* from'user1'@'localhost';'user name'@'host
name';
keyword description:
Insert picture description here
specific operation:
revoke user1 user pair test operation authority database of all tables
. revoke all on test * from ' user1' @ 'localhost';
The user name and host name should be the same as when they were created, and each should be enclosed in single quotes

View permissions

Syntax:
SHOW GRANTS FOR'Username'@'Hostname';

delete users

Syntax:
DROP USER'username'@'hostname';
if you forget the administrator's password, there is a way for general root password solutions. See notes for details.

Guess you like

Origin blog.csdn.net/m0_54814599/article/details/115288135