当前位置:网站首页>MySQL insert data create trigger fill UUID field value
MySQL insert data create trigger fill UUID field value
2022-07-07 10:26:00 【Code slave was born to know only to move forward~】
Catalog
3、 Create a trigger to insert data -BEFORE
1、 Create test table
DROP TABLE IF EXISTS `test_uuid`;
CREATE TABLE `test_uuid` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ' Primary key ',
`uuid` varchar(36) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT 'uuid',
`content` varchar(250) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT ' Content ',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT ' Creation time ',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
2、 Trigger basic syntax
The syntax is as follows :
CREATE < Trigger Name > < BEFORE | AFTER >
<INSERT | UPDATE | DELETE >
ON < Table name > FOR EACH Row< Trigger body >
The grammar is as follows .
2.1、 Trigger Name
Trigger name , Triggers must have unique names in the current database . If you want to create in a specific database , The name should be preceded by the name of the database .
2.2、INSERT | UPDATE | DELETE
Triggering event , Used to specify the type of statement that activates the trigger .
Be careful : The execution times of the three triggers are as follows .
INSERT: Activate trigger when a new row is inserted into the table . for example ,INSERT Of BEFORE Triggers can not only be MySQL Of INSERT Statement activation , Can also be LOAD DATA Statement activation .
DELETE: Activates the trigger when a row of data is deleted from the table , for example DELETE and REPLACE sentence .
UPDATE: Activates a trigger when a row of data in a table is changed , for example UPDATE sentence .
2.3、BEFORE | AFTER
BEFORE and AFTER, When the trigger is triggered , Indicates that the trigger is triggered before or after the statement that activates it . Verify whether the new data meets the conditions , Then use BEFORE Options ; If you want to make a few or more changes after the statement that activates the trigger executes , It is usually used AFTER Options .
2.4、 Table name
The name of the table associated with the trigger , This table must be permanent , Triggers cannot be associated with temporary tables or views . The trigger is activated only when the trigger event on this table occurs . The same table cannot have two triggers with the same trigger time and event . for example , For a data table , You can't have two at the same time BEFORE UPDATE trigger , But there can be one BEFORE UPDATE Trigger and a BEFORE INSERT trigger , Or a BEFORE UPDATE Trigger and a AFTER UPDATE trigger .
2.5、 Trigger body
Trigger action body , Contains the... To be executed when the trigger is activated MySQL sentence . If you want to execute multiple statements , You can use BEGIN…END Compound statement structure .
2.6、FOR EACH ROW
Generally, it refers to line level trigger , For each line affected by the trigger event, activate the action of the trigger . for example , Use INSERT Statement inserts multiple rows of data into a table , The trigger will execute the corresponding trigger action for each row of data insertion .
Be careful : Each table supports INSERT、UPDATE and DELETE Of BEFORE And AFTER, So each table supports at most 6 Trigger . Only one trigger per event per table is allowed at a time . A single trigger cannot be associated with multiple events or tables .
in addition , stay MySQL in , If you need to view the existing triggers in the database , You can use SHOW TRIGGERS sentence .
3、 Create a trigger to insert data -BEFORE
CREATE TRIGGER addUuid BEFORE INSERT ON `test`.`test_uuid`
FOR EACH ROW BEGIN
IF new.uuid = '' OR new.uuid is NULL THEN SET new.uuid = UUID();
END IF;
END
3.1 Insert a piece of data
INSERT INTO `test`.`test_uuid`( `content`, `create_time`) VALUES ( ' View insert generation uuid', now());
You can see uuid Automatically filled .
边栏推荐
- 每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
- ArcGIS operation: converting DWG data to SHP data
- The method of word automatically generating directory
- 2022.7.3DAY595
- Prototype object in ES6
- Use of JSON extractor originals in JMeter
- [email protected] can help us get the log object quickly
- Jump to the mobile terminal page or PC terminal page according to the device information
- Leetcode exercise - 113 Path sum II
- Pdf document signature Guide
猜你喜欢
String formatting
【acwing】786. Number k
Postman interface test VI
【acwing】789. Range of numbers (binary basis)
SolidWorks工程图中添加中心线和中心符号线的办法
嵌入式背景知识-芯片
[STM32] solution to the problem that SWD cannot recognize devices after STM32 burning program
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
Pdf document signature Guide
[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树
随机推荐
JMeter about setting thread group and time
leetcode-560:和为 K 的子数组
ArcGIS operation: batch modify attribute table
IPv4套接字地址结构
table宽度比tbody宽度大4px
嵌入式背景知识-芯片
Mongodb creates an implicit database as an exercise
String formatting
P1223 排队接水/1319:【例6.1】排队接水
IO模型复习
电表远程抄表拉合闸操作命令指令
. Net configuration system
Hdu-2196 tree DP learning notes
Learning records - high precision addition and multiplication
leetcode-304:二维区域和检索 - 矩阵不可变
Talking about the return format in the log, encapsulation format handling, exception handling
Postman interface test VII
The mobile terminal automatically adjusts the page content and font size by setting rem
Vs code specifies the extension installation location
C logging method