133 MySQL view, the transaction, the index (very important)

First, view: view

  1. A view is stored in memory temporary table
  2. Creating the view-dependent select statement, the select statement is the result of the operation parameter table
  3. View support data additions and deletions to change search
  4. View do not allow changes to the field of view of the table
  5. A view not only to support the creation, update and deletion is also supported
# 数据依赖:单表emp

# 语法
# 创建视图
mysql>: create view 视图名[(别名们)] as select 语句;
eg>: create view v1 as select dep, max(salary) from emp group by dep;

# 创建或替换视图
mysql>: create or replace view 视图名[(别名们)] as select 语句;
eg>: create or replace view v1(dep_name, max_salary) as select dep, max(salary) from emp group by dep;

# 修改视图
mysql>: alter view 视图名[(别名们)] as select 语句;
eg>: alter view v1(name, salary) as select dep, max(salary) from emp group by dep;

# 删除视图
mysql>: drop view 视图名
eg>: drop view v1;

Deletions view change search

CRUD operations can be mapped directly to view the real table (essentially real operating table)

# 操作视图等于操作真实表

# 增,增加数据
insert into v1(name,salary) values('yangsir', 1.11);    

# 删,删除视图记录
delete from v1 where id=1;

# 改,修改视图数据
update v1 set salary=salary+1 where id=1;

# 查, 查看视图数据
select * from v1;

# 总结:操作视图,会影响真实表,反之也会影响
select * from emp;

Summary: Action view, will affect the real table, will also affect the contrary view

Second, the transaction

  • Services : Some services usually require multiple sql participation, involvement sql will execute a whole parameter, which we call the whole transaction
  • In short: the transaction - is to protect the sql statement executed multiple

Four characteristics Affairs

  1. Atomic : A transaction is a set of indivisible units, at the same time either succeed or not succeed at the same time
  2. Consistency : Data integrity should be the same thing before and after the (database integrity: If the database at a certain point in time, all the data are in line with all the constraints, called for the integrity of the database state)
  3. Isolation : Isolation of things means that when a plurality of users concurrent access to data, a user can not be firm things interfere with other users, data between a plurality of concurrent transactions to be separated from each other
  4. Persistence : Persistence means that once a thing is submitted, it changed data is permanent, then even if the database fails nor should it have any impact
# 语法
begin;  # 开启事务
update emp set salary=salary+1 where id=2;
update emp set salary=salary-1 where id=3;
commit; # 确认无误,提交事务

rollback;   # 确认有误,回滚

Third, the index

Index is the key - key

Index can greatly speed up queries

1)键 是添加给数据库表的 字段 的
2)给表创建 键 后,该表不仅会形参 表结构、表数据,还有 键的B+结构图
3)键的结构图是需要维护的,在数据完成增、删、改操作时,只要影响到有键的字段,结构图都要维护一次
    所以创建键后一定会降低 增、删、改 的效率
4)键可以极大的加快查询速度(开发需求中,几乎业务都和查有关系)
5)建立键的方式:主键、外键、唯一键、index(普通索引,加快普通数据的查询速度)

Guess you like

Origin www.cnblogs.com/XuChengNotes/p/11595343.html