mysql触发器实例_MySQL
测试表1
DROP TABLE IF EXISTS test; CREATE TABLE test ( id bigint(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(100) NOT NULL DEFAULT '', type varchar(100), create_time datetime, PRIMARY KEY (ID) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
测试表2
DROP TABLE IF EXISTS test_hisy; CREATE TABLE test_hisy ( id bigint(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(100) NOT NULL DEFAULT '', type varchar(100), create_time datetime, operation varchar(100) COMMENT '操作类型', PRIMARY KEY (ID) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
insert触发器
表test新增记录后,将type值为“1”的记录同时插入到test_hisy表中(AFTER INSERT:录入后触发, BEFORE INSERT:录入前触发)
DELIMITER //DROP TRIGGER IF EXISTS t_after_insert_test//CREATE TRIGGER t_after_insert_testAFTER INSERT ON testFOR EACH ROWBEGIN IF new.type='1' THEN insert into test_hisy(name, type, create_time, operation) values(new.name, new.type, new.create_time, 'insert'); END IF;END;//
update触发器
表test修改时,若type值为“2”则将修改前的记录同时插入到test_hisy表中(AFTER UPDATE:修改后触发, BEFORE UPDATE:修改前触发)
DELIMITER //DROP TRIGGER IF EXISTS t_before_update_test//CREATE TRIGGER t_before_update_testBEFORE UPDATE ON testFOR EACH ROWBEGIN IF new.type='2' THEN insert into test_hisy(name, type, create_time, operation) values(old.name, old.type, old.create_time, 'update'); END IF;END;//
delete触发器
表test删除记录前,将删除的记录录入到表test_hisy中(AFTER DELETE:删除后触发, BEFORE DELETE:删除前触发)
DELIMITER //DROP TRIGGER IF EXISTS t_before_delete_test//CREATE TRIGGER t_before_delete_testBEFORE DELETE ON testFOR EACH ROWBEGIN insert into test_hisy(name, type, create_time, operation) values(old.name, old.type, old.create_time, 'delete');END;//

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

"DebianStrings" is not a standard term, and its specific meaning is still unclear. This article cannot directly comment on its browser compatibility. However, if "DebianStrings" refers to a web application running on a Debian system, its browser compatibility depends on the technical architecture of the application itself. Most modern web applications are committed to cross-browser compatibility. This relies on following web standards and using well-compatible front-end technologies (such as HTML, CSS, JavaScript) and back-end technologies (such as PHP, Python, Node.js, etc.). To ensure that the application is compatible with multiple browsers, developers often need to conduct cross-browser testing and use responsiveness

Dockerfile Best Practice for Building LNMP Environment Learning During Docker, many developers try to build their own LNMP (Linux, Nginx, MySQL, PHP)...

Comparison of Redis queues and MySQL stability: Why is Redis prone to data loss? In the development environment, using PHP7.2 and ThinkPHP frameworks, we often face the choice of cooperation...

Using Django and MySQL to process large data volumes When using Django and MySQL databases, if your data volume reaches hundreds of thousands to one or two million...

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

This article discusses how to optimize website performance on Debian systems. "DebianStrings" is not a standard term and may refer to tools or technologies used in Debian systems to improve website performance. The following are some practical tips: 1. It is recommended to use the Pagoda panel to simplify the installation and configuration process for web server and PHP environment configuration. It is recommended to install Nginx1.22.1 as the web server, PHP8.2 as the script interpreter, and MySQL10.7.3-MariaDB as the database system. Be sure to enable the necessary PHP extensions, such as fileinfo, opcache, memcached, red
