当前位置:网站首页>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 .
边栏推荐
- Study summary of postgraduate entrance examination in September
- AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
- Factorial implementation of large integer classes
- MCU is the most popular science (ten thousand words summary, worth collecting)
- Embedded background - chip
- [second on] [jeecgboot] modify paging parameters
- 对word2vec的一些浅层理解
- XML configuration file parsing and modeling
- @Transcation的配置,使用,原理注意事项:
- Fiddler break point
猜你喜欢
STM32 ADC和DMA
Programming features of ISP, IAP, ICP, JTAG and SWD
Use of JSON extractor originals in JMeter
Adb 实用命令(网络包、日志、调优相关)
ArcGIS operation: batch modify attribute table
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
Use the fetch statement to obtain the repetition of the last row of cursor data
Remote meter reading, switching on and off operation command
Postman interface test III
Word自动生成目录的方法
随机推荐
XML configuration file parsing and modeling
PDF文档签名指南
STM32 ADC and DMA
Vs code specifies the extension installation location
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
@Transcation的配置,使用,原理注意事项:
Guid primary key
【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
STM32基础知识—内存映射
C logging method
Sword finger offer 38 Arrangement of strings [no description written]
IPv4 socket address structure
ORM -- grouping query, aggregation query, query set queryset object properties
STM32产品介绍
Kotlin实现微信界面切换(Fragment练习)
Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
ORM model -- associated fields, abstract model classes
UnityWebRequest基础使用之下载文本、图片、AB包
MCU与MPU的区别