当前位置:网站首页>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;
END3.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 .
边栏推荐
- @Transcation的配置,使用,原理注意事项:
- Smart city construction based on GIS 3D visualization technology
- SQLyog数据库怎么取消自动保存更改
- P2788 数学1(math1)- 加减算式
- This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
- [second on] [jeecgboot] modify paging parameters
- Embedded background - chip
- Adb 实用命令(网络包、日志、调优相关)
- 大整数类实现阶乘
- Appx代碼簽名指南
猜你喜欢

深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!

Postman interface test VII

PDF文档签名指南
![[sword finger offer] 42 Stack push in and pop-up sequence](/img/f4/eb69981163683c5b36f17992a87b3e.png)
[sword finger offer] 42 Stack push in and pop-up sequence

Guide de signature du Code Appx

ArcGIS operation: converting DWG data to SHP data

Postman interface test IV

Adb 实用命令(网络包、日志、调优相关)

Appx代码签名指南

01 use function to approximate cosine function (15 points)
随机推荐
Can I open a stock trading account online? Is it safe
Review of the losers in the postgraduate entrance examination
Multisim--软件相关使用技巧
Use of JSON extractor originals in JMeter
leetcode-304:二维区域和检索 - 矩阵不可变
Study summary of postgraduate entrance examination in July
对存储过程进行加密和解密(SQL 2008/SQL 2012)
基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS
Appx代碼簽名指南
【剑指Offer】42. 栈的压入、弹出序列
Postman interface test V
01 use function to approximate cosine function (15 points)
Why is the reflection efficiency low?
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
Sword finger offer 38 Arrangement of strings [no description written]
Serial communication relay Modbus communication host computer debugging software tool project development case
STM32 ADC和DMA
The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
求方程ax^2+bx+c=0的根(C语言)
JMeter loop controller and CSV data file settings are used together