1. The background

This article covers views and triggers.

2. View

A view is a virtual table that also has rows and columns. It points to data by reference.

Create a view

CREATE VIEW view_1 AS
  SELECT * FROM tb_table1 WHERE class = 1;
Copy the code

Modify the VIEW using the CREATE OR REPLACE VIEW.

  CREATE OR REPLACE VIEW view_1 AS
    SELECT * FROM tb_table1 WHERE class = 1;
Copy the code

Or use ALTER VIEW to modify the VIEW

ALTER VIEW view_1 AS 
    SELECT * FROM tb_table1 WHERE class = 1;
Copy the code

Delete the view

DROP VIEW IF EXISTS view_1;
Copy the code

3. Trigger

A trigger is an SQL script embedded in MySQL, sort of like a stored procedure. It is triggered by events such as INSERT, UPDATE, DELETE, and so on. When triggers are defined, events are triggered when these statements are executed, and trigger scripts for these events are activated and executed.

3.1 Creating a Trigger

DELIMITER //
CREATE TRIGGER trggger1 
    BEFORE INSERT ON account
    FOR EACH ROW
  BEGIN
    SET @sum = @sum + NEW.money;
  END //
DELIMITER ;
Copy the code

3.2 Delete Trigger

DROP TRIGGER trggger1;

Copy the code

END