MySQL-- multi-table queries, transactions, DCL

1. Multi-table query

1.1. Query Syntax

select
        list of fields to be queried
from
        list of table names
where....

1.2 Classification of multi-table query

  • 笛卡尔积: The set A and set B, and two sets of the composition taken Cartesian product called
  • 做多表查询要消除无用的笛卡尔积

1.2.1 The inner join query

  • 隐式内连接: Conditions of use where the elimination of useless data
  • 显示内连接

1. Syntax: select field list from table 1 inner join table 2 on condition
2.inner may be omitted

  • 内连接查询注意事项:
    Query data from a table in which
    the condition is what
    which fields queries

1.2.2. Outer join query

  • 左外连接: Query is the left table all of the data and their intersection part

1. Syntax: select field list table from the left on the right table left outer join condition;
2.outer may be omitted

  • 右外链接: Query is the right of all data tables and their intersection part

1. Syntax: select field list table from the left on the right table right outer join condition;
2.outer may be omitted

1.2.3. Subqueries

  • 子查询: Loop nested queries, we call this a nested query into sub-queries
  • 子查询的不同情况

1. The results of the sub-query is 单行单列a

  • Subquery 作为条件, using operators (>, <, =, etc.) to judge

2. The results of sub-query is 多行单列a

  • Subquery 作为条件using IN operator to judge

3. The results of the sub-query is 多行多列a

  • Subqueries can 作为一张虚拟table to table query

2. Transaction

2.1. Basic introduction affairs

2.1.1. Transaction concept

事务: If a contains 多个步骤business operations 被事务管理(transaction management to be the first to open the transaction), then either of these operations 同时成功, success 提交, or 同时失败failure to回滚

For example: Joe Smith John Doe to transfer 500

  • 1. Joe Smith query account the amount of excess 500
  • 2. Joe Smith cut the amount of accounts 500
  • 3. John Doe account value plus 500

If you are into a whole transaction management, three operations, while either succeed or fail at the same time, you can prevent some exceptions, such as the second step operation is complete there is a problem, disrupting the entire event, so Joe Smith account less 500 Li four but no more than 500 accounts, it is unreasonable

2.1.2. Transaction Procedure

  • 开启事务:start transaction
  • 提交事务: Commit (manual submission)
  • 回滚事务:roll back

2.1.3. Rights to

1. The transaction commits Category:

  • 手动Submission: first open transaction, and then commit the use
  • 自动Submission: 在MySQL数据库中,一条DML(增删改)会默认自动提交一次事务The data persistence update

2.查看事务的默认提交方式:select @@autocommit;
Here Insert Picture Description
3.修改事务提交方式:set @@autocommit = 0;

  • Note: Changes to manual submission later, the data must be modified committo take effect, otherwise a close the graphical window and then open the data will return to its original state

2.2. Four features affairs

  • 原子性: Is inseparable smallest operating unit, at the same time either succeed or fail at the same time
  • 持久性: If the transaction once committed or rolled back, the database will save persistent data
  • 隔离性: Independent (isolated) between a plurality of transactions, independently of each other
  • 一致性: The total amount of the transaction data unchanged before and after the operation. Such as transfers, Bahrain account the total amount of the two is still the same

2.3. The transaction isolation level

2.3.1. Isolation level concept

事务的隔离级别: Between multiple transactions are independent of each other, if multiple transactions with a number of operational data can cause some problems, these problems can be solved by setting different levels of isolation

2.3.2. Some problems of the transaction operation data

  • 脏读: It refers to when a transaction is accessing data, and the data has been modified, and this modification has not been submitted to the database, then, another transaction also access the data, and then use this data
  • 不可重复读(虚读): Refers to a transaction, reading the same data multiple times, when the transaction is not over, another transaction also access the same data and made changes, so between the two read data in the first transaction, since modify the second transaction, the first transaction data may be read twice is not the same. This results in a transaction the two read data is not the same, so called non-repeatable read, i.e., can not read the same data content
  • 幻读: Refers to a phenomenon that occurs when a transaction is not performed independently, for example, a first transaction data in the table has been modified, this modification involves the table
    all the data lines. At the same time, also the second transaction to modify the data in this table, this modification is to insert a new row to the table. Then, the user operates the first transaction will occur after the discovery of the table there is no modification of data rows, as if all the data make changes to the line is the same illusion occurs

2.3.3. The four isolation levels

  • 读未提交: Read uncommitted, problem: dirty reads, non-repeatable read, phantom read all the modifications themselves
  • 读已提交: Read committed, problem: non-repeatable read, phantom read
  • 可重复读: Repeatable read, problem: Magic Reading (MySQL by default)
  • 串行化: Serializable, no problem

隔离级别越高,安全系数越高,但是效率越来越低, So we have to choose the appropriate relative isolation levels, both to ensure security and ensure efficiency

2.3.4. Query isolation level

select @@tx_isolation;

2.3.5. Modify isolation level

level set global transaction isolation level string;

  • After modifying the isolation level will be able to shut down and re-enter into force SQLyog

3.DCL: user management, authorization

3.1. User Management

  • 添加用户: Create user 'username' @ 'hostname' identified by 'password';
  • 删除用户: Drop user 'username' @ 'hostname';
  • 修改用户密码
  • Method a: update user set password = password ( 'new password') where user = 'username';
  • Method two: set password for 'username' @ 'hostname' = password ( 'new password');

! ! ! mysql中忘记了root用户的密码
1. Run as administrator cmd, net stop mysql, mysql stop service
2. Use no authentication start mysql service: mysqld --skip-grant-tables, press Enter, then open a new cmd window, enter mysql Enter , you can log in successfully
3.use mysql, Enter, and then update user set password = password ( 'new password') where user = 'root' , change your password
4. Close the two windows, open the task Manager to end the mysql manual .exe process
5. start mysql service, using your new password

  • 查询用户

1. * Switch to the MySQL database: MySQL use;
2. * query the user table: select * from user;
Note: The wildcard "%" means that the user can use the database to log on any host

3.2 Rights Management

  • 查询权限: Show grants for 'username' @ 'hostname';
  • 授予权限: Grant permissions list on the name of the database table name to 'username' @ 'hostname';.
  • 撤销权限: Revoke permissions list on the name of the database table name from 'username' @ 'hostname';.

Guess you like

Origin blog.csdn.net/LiLiLiLaLa/article/details/93116633