Database knowledge review: Mysql triggers

        It has been a year since Mr. Xiaohong's "Database Principles" course. Everyone must remember adding, deleting, modifying and checking, but the trigger is probably forgotten (at least I gave it back to Mr. Xiaohong). Let's review it with an example. Take a look at the knowledge of trigger triggers.

First, the definition of trigger

        A trigger is a database object related to a table that fires when a defined condition is met and executes the set of statements defined in the trigger. Its definition pseudocode is as follows:

DELIMITER ||
CREATE TRIGGER trigger_name trigger_time trigger_event 
ON table_name FOR EACH ROW 
BEGIN
    trigger_statement;
    ...;
    ...;
END
||
DELIMITER ;

Interpretation:
        DELIMITER ||: Change the end symbol to || to avoid conflict with the line break in trigger_statement;
        trigger_name: trigger name trigger_time
        : trigger timing: BEFORE, that is, before the event or AFTER, that is, after the event
        trigger_event: triggering event: INSERT , DELETE or UPDATE
        table_name: Indicates the name of the table where the trigger is created, that is, on which table the trigger is created
        FOR EACH ROW: Indicates that any operation on a record that satisfies the trigger event will trigger the trigger
        trigger_statement: The program body of the trigger, It can be one SQL statement or multiple statements
        ||: Indicates that the trigger creation is completed
        DELIMITER ;: After the trigger creation is completed, restore the end symbol back to;

2. Trigger creation instance

-- 创建数据库
create database book_test;
use book_test;
-- 创建图书信息表
create table bookinfo(
book_id int primary key auto_increment,
book_name varchar(20) not null,
price float(6,2) not null,
public_date date not null,
store int not null
);
-- 插入图书数据
insert into bookinfo(book_name,price,public_date,store) values('Android移动应用开发',39.8,'2021-12-26',6);
insert into bookinfo(book_name,price,public_date,store) values('Java程序设计',56.2,'2011-12-26',15);
insert into bookinfo(book_name,price,public_date,store) values('算法与数据结构',20,'2015-02-10',18);
select * from bookinfo
-- 创建日志数据表
CREATE TABLE `logs` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `log` varchar(255) DEFAULT NULL COMMENT '日志说明',
  PRIMARY KEY (`Id`)
)
-- 创建触发器,作用:当在bookinfo中插入一条数记录时,会在logs中生成一条日志信息
DELIMITER ||
CREATE TRIGGER user_log AFTER INSERT 
ON bookinfo FOR EACH ROW 
BEGIN
		DECLARE s1 VARCHAR(40) character set utf8;
		DECLARE s2 VARCHAR(40) character set gbk;
    SET s1 = ' is created';    
		#函数CONCAT将字符串连接  NEW.columnname:新增行的某列数据
		SET s2 = CONCAT(NEW.book_name,s1); 
		INSERT INTO logs(log) values(s2);
END
||
DELIMITER ;
-- 查看创建的触发器
SELECT * FROM information_schema.triggers;
-- 测试触发器
select * from logs;
insert into bookinfo(book_name,price,public_date,store) values('解忧杂货店',48,'2016-03-03',48);
select * from logs;

3. Operation results

1>The trigger is created successfully:

2> The trigger is executed successfully:

Guess you like

Origin blog.csdn.net/qq_43554335/article/details/123258104