Mysql study notes (five: subqueries and advanced usage)

Nine, sub query

1. Subquery Basic Syntax

select * from 表名1 where 字段1 in (select 字段2 from 表名2 where 字段名+限制条件); 当括号内的数据只有一条时可以使用=,数据有多条时用in,一般情况下括号内的查询结果我们要一个字段的数据,即字段2处只填一个字段名

select * from 表名1 where 字段1 not in (select 字段2 from 表名2 where 字段名+限制条件);

2、exists和not exists

select *(某字段) from 表名1 where exists (select 字段2 from 表名2 where 字段名+限制条件); 当括号内的查询结果存在时,显示表1全部(某字段)内容

select * from 表名1 where not exists (select 字段2 from 表名2 where 字段名+限制条件);

10. Advanced usage

1. View (view)

Views can only be used for viewing, and cannot be added, deleted or modified

1.1, view view creation, use, function

The role of the view is to limit, limit what you can see, and protect the privacy of sensitive and important data.

create view 视图名 as select ... from ... ...; 创建视图 (视图的内容就是as后面select语句查询到的数据,as后面可以跟任何select查询语句,即七、八、九部分的所有select语句,但是在视图中使用子查询时要注意视图算法的选择)

select * from 视图名; 查看创建的视图

show tables; 查看当前数据库下的表和视图(我们一般在创建视图时用下划线命名法加前缀ve_来区分表和视图)

desc 视图名/表名; 查看表或视图的结构

show create view 视图名/表名; 查看创建表或视图的SQL语句

show table ststus where comment='view' \G; 一种高B格的查询视图的方法

1.2. Update and delete views

alter view 视图1 as select ... from ... ...; 更改名为视图1的视图的内容为as后面select语句查询到的数据

drop view 视图名; 删除视图

1.3. View algorithm tempptable, merge

When using subqueries in views, pay attention to modifying the view algorithm

create algorithm=视图算法 view 视图名 as select ... from ... ...;

There are two view algorithms, tempptable temporary table algorithm (commonly used) and merge merge algorithm. If you do not specify a view algorithm when creating a view, the default is undefined, that is, undefined.

2. Transaction

Transactions are equivalent to buying and paying for things on Taobao, there is an intermediate transition. The seller will only be paid after the receipt is confirmed. If the product is returned, the seller will not receive the money. It can also be compared to a bank transfer. After both parties confirm that the transfer is confirmed, the money will be transferred. This transitional process in the middle of the operation is called a transaction.

Transactions can only be used when the database engine is InnoDB

2.1、transaction

start transaction; 开启事务

After opening the transaction, you can perform create, drop, alter, insert, delete, update... any operation

update 表1 set 某字段=某字段进行加减等操作 where 字段1=要更新的那组数据对应的字段1的值; 对表1某组数据的某字段进行...的更新

commit; 提交(提交之后改变生效)

rollback; 回滚(退出事务且改变不生效,相当于撤销)(回滚操作只能在commit提交之前做,一旦提交就不能回滚)

2.2, rollback to rollback point

The rollback point is similar to the function of the snapshot in the virtual machine

savepoint 回滚点名;

rollback to 回滚点名;(回滚操作只能在commit提交之前做,一旦提交就不能回滚)

2.3、ACID

The four major characteristics of ACID transactions

auomicity atomicity

consistency consistency

lsolation isolation

durability

3. Index (index)

After setting the index, it will speed up the search and facilitate the search, but the speed of addition, deletion and modification will be slower, and the index will take up more space

Four kinds of indexes: primary key index, unique key index, common index, global index

create index 索引名 on 表名(字段名); 普通索引

create unique index 索引名 on 表名(字段名); 唯一键索引

create primary key index 索引名 on 表名(字段名); 主键索引

alter, drop... modify, delete index

4. Stored procedure

A stored procedure is equivalent to customizing a function of an SQL statement, and you can directly call this function when you want to use it

4.1、delimiter

delimiter //; 设置SQL语句以//结尾(默认是以;为SQL语句的结尾)

4.2、procedure

Write a stored procedure: (first set the SQL statement to end with //, then write)

create procedure 函数名()

begin

想要执行的SQL语句;

end //

Stored procedures can also nest transactions

call 函数名(); 调用存储过程(函数)

drop procedure 函数名; 删除存储过程(函数)

show create procedure 函数名; 查看创建存储过程(函数)的SQL语句

show procedure status \G 显示数据库所有的存储过程(函数)

conclusion 

    When you see this, the column MySQL study notes is over. All of the above are the notes I recorded during my MySQL study. These are just some of the most basic operations of MySQL. If you can master all these notes, it will not be a big problem to complete the internship. If you want to become a professional DBA, then these are far from enough. I hope my sharing can help everyone learn MySQL.

There are 5 blogs in this column. If you need to reprint or quote, please contact me and indicate the source.

Mysql study notes (1: basic operations of databases and tables)_Shipmaster_23's Blog-CSDN Blog

Mysql study notes (two: data operation and data type)_Shipmaster_23's Blog-CSDN Blog

Mysql study notes (three: column attribute integrity and database design thinking)_Shipmaster_23's Blog-CSDN Blog

Mysql study notes (four: single-table query and multi-table query)_Shipmaster_23's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/weixin_63268005/article/details/128638015