View mysql database, the trigger

Original link: http://www.cnblogs.com/dongxixi/p/10885011.html

view

The concept: through a virtual table from the query, and then save that view down

Benefits of view: If you frequently use a virtual table sheets, then it can be saved as a view of the future look directly take this view will be very convenient

View grammar rules:

the Create  View teacher2course AS virtual table

Point of view need to be aware of:

  1, in view of the hard disk only table structure file, there is no table data file, the data from the original table query

  2, a view commonly used in the query, try not to modify the data in the table view, modify data could lead to the original table data problems

 

 

trigger

The concept: When the additions and deletions to the operation of a particular table will automatically trigger the execution of another part of the function of behavior is known triggers

Why Triggers: Triggers specifically for us to change the behavior of additions and deletions to a particular table, actual demand, we may need to monitor the operation of this table and perform some other functions, this time using the flip-flop will be very convenient.

Create trigger syntax rules:

# Insert for
 the Create  the Trigger tri_after_insert_t1 the After INSERT  ON table name for the each Row
 the begin
    sql code. . .
end 
create trigger tri_after_insert_t2 before insert on 表名 for each row
begin
    sql code. . .
end

# For deletion
create trigger tri_after_delete_t1 after delete on 表名 for each row
begin
    sql code. . .
end
create trigger tri_after_delete_t2 before delete on 表名 for each row
begin
    sql code. . .
end

Modifications to #
create trigger tri_after_update_t1 after update on 表名 for each row
begin
    sql code. . .
end
create trigger tri_after_update_t2 before update on 表名 for each row
begin
    sql code. . .
end

Pseudo Case:

# Case
 the CREATE  TABLE cmd (
    id INT PRIMARY KEY auto_increment,
    USER CHAR (32),
    priv CHAR ( 10 )
    cmd CHAR (64),
    SUB_TIME datetime , # commit time
    enum Success ( ' yes ' , ' NO ' ) # 0 represents the execution failed
);

CREATE TABLE errlog (
    id INT PRIMARY KEY auto_increment,
    err_cmd CHAR (64),
    err_time datetime
);

The default delimiter $$ # mysql a terminator; $$ replace
Create  Trigger tri_after_insert_cmd After INSERT  ON cmd for each Row
 the begin 
    IF NEW.success =  ' NO '  the then   # MySQL new record will be encapsulated into objects NEW
         INSERT  INTO errlog (err_cmd, err_time) values (NEW.cmd, NEW.sub_time);
     End  IF ;
 End $$
delimiter; # after the end remember to change it back, or terminator is back on the $$

# Insert a record into the table cmd, fire triggers, decide whether to insert the error log under the conditions of IF
INSERT INTO cmd (
    USER,
    priv,
    cmd,
    sub_time,
    success
)
VALUES
    ('egon','0755','ls -l /etc',NOW(),'yes'),
    ('egon','0755','cat /etc/passwd',NOW(),'no'),
    ('egon','0755','useradd xxx',NOW(),'no'),
    ('egon','0755','ps aux',NOW(),'yes');

# Query table record errlog
select * from errlog;
# Delete trigger
drop trigger tri_after_insert_cmd;

 

Reproduced in: https: //www.cnblogs.com/dongxixi/p/10885011.html

Guess you like

Origin blog.csdn.net/weixin_30888413/article/details/94816235