Mysql triggers are used in PHP projects to back up, restore and clear information

Mysql triggers are used in PHP projects to back up, restore and clear information

Examples:

Through the PHP background code, you can delete employee information, restore deleted employee information (similar to restoring employee information from the recycle bin), and clear deleted employees (similar to the function of clearing the reply station).

Ideas:

There must be an employee table and an employee backup table; for backup, use triggers to import the information in the employee table into the backup table before clicking the delete button to execute the delete function, thus achieving the backup effect; for recovery, use triggers on the backup table to delete the data in the backup table, and import the data into the employee table at the same time; for clearing, use the truncate method to completely clear the data in the backup table and release memory, and this method will not call triggers for data deletion. Without further ado, let me get straight to the point.

Step 1: Create tables, employee table, and employee backup table.

CREATE TABLE `employee` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `denumber` varchar(255) DEFAULT '0',
 `idnumber` varchar(255) DEFAULT '0',
 `worknumber` varchar(255) DEFAULT '1',
 `pwd` varchar(255) DEFAULT NULL,
 `emname` varchar(255) DEFAULT '0',
 `tel` varchar(255) DEFAULT '0',
 `salary` int(255) DEFAULT '0',
 `entrytime` varchar(255) DEFAULT '0',
 `orderpaixu` int(255) DEFAULT '1',
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=100 DEFAULT CHARSET=utf8

This is the employee list

CREATE TABLE `employeebackup` (
 `id` int(11) NOT NULL,
 `denumber` varchar(255) DEFAULT NULL,
 `idnumber` varchar(255) DEFAULT NULL,
 `worknumber` varchar(255) DEFAULT NULL,
 `pwd` varchar(255) DEFAULT NULL,
 `emname` varchar(255) DEFAULT NULL,
 `tel` varchar(255) DEFAULT NULL,
 `salary` int(255) DEFAULT NULL,
 `entrytime` varchar(255) DEFAULT NULL,
 `orderpaixu` int(255) DEFAULT NULL,
 `deletetime` datetime DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

The employee backup table has an additional field called deletetime to record the deletion time.

Step 2: Back up and create a trigger for the employee table (for information about triggers, you can view my other blog http://www.cnblogs.com/liebagefly/p/7517998.html), and import the information in the employee table into the backup table before clicking the delete button to execute the delete function.

Trigger sql code:

CREATE trigger deletesemployee before delete on employee
for each ROW
begin 
 insert into employeebackup (id,denumber,idnumber,worknumber,pwd,emname,tel,salary,entrytime,orderpaixu,deletetime)
values(OLD.id,OLD.denumber,OLD.idnumber,OLD.worknumber,OLD.pwd,OLD.emname,OLD.tel,OLD.salary,OLD.entrytime,OLD.orderpaixu,NOW());
end

PHP background method, the framework I use is yii2.

 public function actionEmployeedel($id)
 {
  Employee::findOne($id)->delete();
  return $this->redirect(['employeemanage']);
 }

Step 3: Restore, restore the deleted information, use triggers on the backup table, delete the data in the backup table, and import the data into the employee table while deleting it.

Trigger sql code:

CREATE trigger deletesemployeebackup before delete on employeebackup
for each ROW
begin 
  insert into employee (id,denumber,idnumber,worknumber,pwd,emname,tel,salary,entrytime,orderpaixu)
values(OLD.id,OLD.denumber,OLD.idnumber,OLD.worknumber,OLD.pwd,OLD.emname,OLD.tel,OLD.salary,OLD.entrytime,OLD.orderpaixu);
end

PHP code

public function actionRecoveremployeedel($id)
 {
  Employeebackup::findOne($id)->delete();
  return $this->redirect(['recoveremployee']);
 }

In addition to backup, sometimes a clearing function is also required. Use the truncate method to completely clear the data in the backup table and release memory. This method will not call triggers for data deletion.

The original way of calling SQL in the background of Yii2 is to clear all deleted users.

public function actionDropemployeedel()
{
 Yii::$app->db->createCommand('truncate table employeebackup')
  ->execute();
 return $this->redirect(['recoveremployee']);
}

The above is a simple usage of MySQL trigger in a small project.

You may also be interested in:
  • An article to give you a deep understanding of Mysql triggers
  • Use of MySQL triggers
  • Detailed explanation of MySQL database triggers
  • MySQL trigger syntax and application examples
  • Detailed explanation of MySql view trigger stored procedure
  • Detailed explanation of the idea of ​​MySQL trigger detecting a statement in real time for backup and deletion

<<:  Detailed understanding and comparative analysis of servers Apache, Tomcat and Nginx

>>:  Nodejs Exploration: In-depth understanding of the principle of single-threaded high concurrency

Recommend

jQuery plugin to implement stacked menu

A jQuery plugin every day - stacked menu, for you...

HTML Tutorial: Collection of commonly used HTML tags (6)

These introduced HTML tags do not necessarily ful...

Thinking about grid design of web pages

<br />Original address: http://andymao.com/a...

MySQL 5.7.17 winx64 installation and configuration method graphic tutorial

Windows installation mysql-5.7.17-winx64.zip meth...

Ten important questions for learning the basics of Javascript

Table of contents 1. What is Javascript? 2. What ...

IIS 7.5 uses URL Rewrite module to achieve web page redirection

We all know that Apache can easily set rewrites f...

Summary of several common methods of JavaScript arrays

Table of contents 1. Introduction 2. filter() 3. ...

5 Ways to Send Emails in Linux Command Line (Recommended)

When you need to create an email in a shell scrip...

CocosCreator Skeleton Animation Dragon Bones

CocosCreator version 2.3.4 Dragon bone animation ...

The difference and reasons between the MySQL query conditions not in and in

Write a SQL first SELECT DISTINCT from_id FROM co...

Sample code for highlighting search keywords in WeChat mini program

1. Introduction When you encounter a requirement ...

Analysis of the difference between absolute path and relative path in HTML

As shown in the figure: There are many files conne...

How to directly reference vue and element-ui in html

The code looks like this: <!DOCTYPE html> &...

Mysql NULL caused the pit

Using NULL in comparison operators mysql> sele...