MySQL views, triggers, transactions

 
A view
1. What is the view of 
view is obtained by querying a virtual table, and then saved, can be used directly next 2, why should view 
if you frequently use a virtual table, you can not repeat the query

3, How to view
create view teacher2course as
select * from teacher inner join course
on teacher.tid = course.teacher_id;

drop view teacher2course;
Stressed 
1, in the hard disk, view the table structure only files, not data files Table 
2, the view is often used for flashback, try not to modify the data view

Second, the flip-flop
 

  1, in the case of satisfying a particular table data add, delete, change, automatically trigger the trigger function is called 

  2. Why do you use the trigger?

  A: We are a trigger specifically for a table of data by insert, delete delete, change update behavior, once the implementation of such acts will trigger execution of a trigger that automatically runs another section of the code sql

- Syntax: 
DELIMITER // 
the Create  the Trigger the Trigger name the After / the before    INSERT / the Delete / Update    ON table name for the each Row
 the begin 
    SQL code. . . 
END  // 
DELIMITER; 

- explain: 
- the After trigger event after the execution of the trigger event before before execution 
- the trigger event insert delete update operation, trigger trigger execution 
- DELIMITER: Delimiter
The CREATE  TABLE cmd ( 
    ID the INT  a PRIMARY  KEY AUTO_INCREMENT,
     the USER  CHAR ( 32 ), 
    PRIV CHAR ( 10 ), 
    cmd CHAR ( 64 ), 
    SUB_TIME datetime , # submission time 
    Success enum ( ' Yes ' , ' NO ' ) # 0 Representative fails 
); 

the CREATE  TABLE errlog ( 
    ID the INT  a PRIMARY  KEY AUTO_INCREMENT, 
    err_cmd CHAR( 64 ), 
    err_time datetime 
); 

DELIMITER $$ 
Create  Trigger tri_after_insert_cmd After INSERT  ON cmd for each Row
 the begin 
    IF NEW.success =  ' NO '  the then 
        INSERT  INTO errlog (err_cmd, err_time) values (NEW.cmd, NEW.sub_time);
     End  iF ;
 End $$ 
DELIMITER; 


- when using the update statement, when modifying the original data when the data table data table is modified with respect to the original table is modified piece of data objects OLD, 
- modifying after the data is changed and the piece of data objects is NEW 
 
-To insert a record into table cmd, triggered flip-flop, decide whether to insert an error log according to the condition of the IF 
the INSERT  the INTO cmd (
     the USER , 
    PRIV, 
    cmd, 
    SUB_TIME, 
    Success 
) 
the VALUES 
    ( ' Egon ' , ' 0755 ' , ' LS -l / etc ' , the NOW (), ' Yes ' ), 
    ( ' Egon ' , ' 0755 ' , ' CAT / etc / the passwd ' , the NOW (), ' NO ' ), 
    ( 'there','0755','useradd xxx',NOW(),'no'),
    ('egon','0755','ps aux',NOW(),'yes');

drop trigger tri_after_insert_cmd;
Examples of triggers

 


Third, the transaction
basic introduction 1. The transaction

1. Concept: * business operations if a multi-step, the transaction management, these operations either at the same time succeed or fail at the same time. 2. Operation: 1. Open Transaction: Start Transaction; 2 rollback: ROLLBACK; 3. Submit: the commit; 3. Examples:







    TABLE Account the CREATE ( 
            ID a PRIMARY KEY the AUTO_INCREMENT the INT, 
            NAME VARCHAR (10), 
            Balance DOUBLE 
        ); 
        - Add Data 
        INSERT INTO account (NAME, balance) VALUES ( 'zhangsan', 1000), ( 'lisi', 1000); 
        * the FROM account the SELECT; 
        the UPDATE account Balance the SET = 1000; 
        - Doe Zhang to transfer 500 yuan 
        - 0. transaction open 
        the START tRANSACTION; 
        - 1. Zhang account -500 
        the UPDATE = account Balance Balance the SET - 500 the WHERE NAME = 'zhangsan'; 
        - 2. John Doe account +500 
        - wrong ... 
        the UPDATE the SET account Balance Balance + = 500 the WHERE NAME = 'Lisi';
        
        
        
        - Discover perform no problem, commit the transaction 
        COMMIT; 
        
        - found a problem, roll back the transaction 
        ROLLBACK;

 


4. MySQL database default auto-commit transaction * transactions submitted in two ways: * Automatic submission: * MySQL is automatically submitted * a DML (additions and deletions) statement will automatically submit a transaction. * Manual submission: * the Oracle database default is to commit the transaction manually * need to enable the transaction, and then submitted * Modify the default transaction of submission: * View the default transaction of submission: the SELECT @@ autocommit; - 1 representative of Auto submit on behalf of the manual 0 submit * modify the default submission: the SET @@ autocommit = 0;










2. 事务的四大特征 ACID:
1. 原子性:是不可分割的最小操作单位,要么同时成功,要么同时失败。
2. 持久性:当事务提交或回滚后,数据库会持久化的保存数据。
3. 隔离性:多个事务之间。相互独立。
4. 一致性:事务操作前后,数据总量不变
3. 事务的隔离级别(了解)
* 概念:多个事务之间隔离的,相互独立的。但是如果多个事务操作同一批数据,则会引发一些问题,设置不同的隔离级别就可以解决这些问题。
* 存在问题:
1. 脏读:一个事务,读取到另一个事务中没有提交的数据
2. 不可重复读(虚读):在同一个事务中,两次读取到的数据不一样。
3. 幻读:一个事务操作(DML)数据表中所有记录,另一个事务添加了一条数据,则第一个事务查询不到自己的修改。
* 隔离级别
1. read uncommitted:读未提交
* 产生的问题:脏读、不可重复读、幻读
2. read committed:读已提交 (Oracle)
* 产生的问题:不可重复读、幻读
3. repeatable read:可重复读 (MySQL默认)
* 产生的问题:幻读
4. serializable:串行化
* 可以解决所有的问题

* 注意:隔离级别从小到大安全性越来越高,但是效率越来越低
* 数据库查询隔离级别:
* select @@tx_isolation;
* 数据库设置隔离级别:
* set global transaction isolation level 级别字符串;

* 演示
        set global transaction isolation level read uncommitted;
        start transaction;
        -- 转账操作
        update account set balance = balance - 500 where id = 1;
        update account set balance = balance + 500 where id = 2;

 

 

 

Guess you like

Origin www.cnblogs.com/timetellu/p/11614325.html