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:
|
<<: Detailed understanding and comparative analysis of servers Apache, Tomcat and Nginx
>>: Nodejs Exploration: In-depth understanding of the principle of single-threaded high concurrency
This article shares the MySQL precompilation func...
This article example shares the specific code of ...
Preface Before talking about covering index, we m...
Real-time replication is the most important way t...
Clustering is actually relative to the InnoDB dat...
About semantics Semantics is the study of the rel...
Update: Recently, it was discovered that the serv...
Key Points The CSS resize property allows you to ...
This article mainly introduces the solution to th...
Code implementation: Copy code The code is as fol...
Supervisor is a very good daemon management tool....
1. [admin@JD ~]$ cd opt #Enter opt in the root di...
Table of contents 1. Global Guard 1. Global front...
Preface Since the types of the same fields in the...
WeChat applet uniapp realizes the left swipe to d...