Triggers, views, functions, stored procedures

MySQL trigger

Triggers are used to "automatically" perform certain operations during certain operations.

After insert delete update has set the trigger, executing the insert delete update operation will automatically trigger the set content.

​ Note: A table can have up to 6 triggers (3*2, three operations * two times (before|after))

Define trigger

grammar:create trigger 触发器名 before|after 事件 on 表名 for each row 触发器语句;

  • The trigger name is recommended to be trigger_xxx, which is easy to distinguish, and the trigger name cannot be repeated.
  • before|after represents the execution time of the trigger statement. If it is before, the trigger statement is executed before the insert|delete|update operation; after is after.
  • The event is one of the insert delete update operations.
  • for each row is a trigger for performing corresponding operations on behalf of any record.
  • The trigger statement is the statement to be executed when the trigger fires.

Multiple trigger statements

Need to include multiple statements in the begin end

CREATE TRIGGER 触发器名

before|after

insert|update|delete

ON 表名FOR EACH ROW

BEGIN触发语句1;触发语句2;...触发语句n;

END;

In MySQL; the default is the end of statement, there are problems when using the command line

When using multiple trigger statements on the command line, you should first modify the statement terminator ($$)

Instance

DELIMITER @@
CREATE TRIGGER trigger_a_insert AFTER UPDATE ON `account` FOR EACH ROW
BEGIN
INSERT INTO account_history(account_name,changed_cash) VALUES('cust','10000');
END@@

DELIMITER ;
UPDATE account SET cash=cash+1000 WHERE `name`='A';

MySQL view trigger

show triggers;
View all triggers
select * from triggers;
View triggers table in information_schema
PS:加where条件进行条件查询
show create trigger 触发器名;
View trigger creation statement
PS: delete trigger: drop trigger trigger name;

New and old records in MySQL triggers

There are three main trigger events:

  • insert: new record, no old record;
  • delete: delete old records, no new records;
  • update: There are new records after changes and old records that have been changed;

You can use old|new to reference these records

  • After for each row (that is, the trigger statement)

PS: At this time, there are not multiple trigger statements, no begin...end;

Instance

CREATE TRIGGER trigger_a_insert AFTER INSERT ON account FOR EACH ROW
INSERT INTO account_history(account_name,changed_cash)
VALUES(new.name,new.cash);

INSERT INTO account(NAME,cash) VALUES('abc','100');
CREATE TRIGGER trig_a_update AFTER UPDATE ON account FOR EACH ROWINSERT INTO account_history(account_name,changed_cash)VALUES(CONCAT('old:',old.name,';new:',new.name),new.cash-old.cash);

UPDATE account SET cash=cash-1000,NAME='icbc' WHERE NAME='A';

MySQL view

View sql statement query is stored in the database
is essentially the result of the query, stored in a temporary space
is a virtual table, the contents of the query definition
view had no physical storage in the database, only the equivalent of a temporary table
for which For the referenced base table, the role of the MySQL view is similar to filtering. It
can be one table or multiple tables

Advantages of the view:

  • Simplify, data WYSIWYG
  • Security, users can only query or modify the data they can see
  • Logical independence can shield the impact of changes in the real table structure

Disadvantages of the view:
poor performance and inconvenient modification

The role of the view:

Function 1: Improve reusability.
For complex statements that require frequent queries, using views can be equivalent to storing the statements as temporary tables. When calling, you only need to query from the view location.
Function 2: External interfaces are stable
when the underlying tables of the database change , The original table may not exist, but there is no such problem when using the view.
Function 3: Improve security and
only open specific fields to external interfaces.
Function 4: The purpose is clearer and the data is clearer.

View creation and use:

Create grammar
Grammar structure:

{ CREATE|REPLACE } [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
VIEW 视图名 [{属性列名}] AS SELECT_STATEMENT(查询语句)
[WITH [CASCADED | LOCAL |] CHECK OPTION]

ALGORITHM: View algorithm (1, default; 2, merge (execute after the view and outer statement are merged); 3. save the result in a temporary table)

Stored procedure

Stored procedure syntax:

CREATE procedure `存储过程名` (参数列表)begin	sql语句end;

Instance

DELIMITER @@
CREATE PROCEDURE test()BEGINIF 1>0 THEN SELECT 1 ;END IF;END@@DELIMITER ;

CALL test;
DELIMITER @@
CREATE PROCEDURE testcase(IN VALUE INT(11))
BEGIN
CASE VALUE
    WHEN 1 THEN SELECT 1+1;
    WHEN 2 THEN SELECT 1+2;
    WHEN 3 THEN SELECT 1+3;
    ELSE SELECT 1+VALUE;
    END CASE;
END@@
DELIMITER ;


CALL testcase(6);
DELIMITER @@
CREATE PROCEDURE testwhile()
BEGIN
SET @i:=0;
SET @sum:=0;
WHILE @i<=10 DO
SET @sum:=@sum+@i;
SET @i:=@i+1;
END WHILE;
END@@
DELIMITER ;

CALL testwhile;

UDF custom function

UDF syntax:

create function 函数名([参数列表]) returns 数据类型begin sql语句; return 值;end;
DELIMITER @@
CREATE FUNCTION testfail() RETURNS VARCHAR(10)
BEGIN
DECLARE num,SUM INT(11) DEFAULT 0;
WHILE num<=100 DO
SET num=num+1;
SET SUM=SUM+num;
END WHILE;
RETURN SUM;
END@@
DELIMITER ;

SELECT testfail();

Stored procedures and UDF functions

Similarities between stored procedures and functions:

  1. It is a collection of a series of sql statements, which are executed at one time when calling, similar to the concept of methods in java
  2. Are extensions to MySQL functions

The difference between stored procedures and functions:

  1. The stored procedure has no return value, the function has a return value
  2. The stored procedure focuses on the execution process, the function focuses on the return value

Guess you like

Origin blog.csdn.net/zmzdmx/article/details/108119842