当前位置:网站首页>mysql插入数据创建触发器填充uuid字段值
mysql插入数据创建触发器填充uuid字段值
2022-07-07 07:56:00 【码奴生来只知道前进~】
目录
1、创建测试表
DROP TABLE IF EXISTS `test_uuid`;
CREATE TABLE `test_uuid` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`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 '内容',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;2、触发器基本语法
语法格式如下:
CREATE <触发器名> < BEFORE | AFTER >
<INSERT | UPDATE | DELETE >
ON <表名> FOR EACH Row<触发器主体>语法说明如下。
2.1、触发器名
触发器的名称,触发器在当前数据库中必须具有唯一的名称。如果要在某个特定数据库中创建,名称前面应该加上数据库的名称。
2.2、INSERT | UPDATE | DELETE
触发事件,用于指定激活触发器的语句的种类。
注意:三种触发器的执行时间如下。
INSERT:将新行插入表时激活触发器。例如,INSERT 的 BEFORE 触发器不仅能被 MySQL 的 INSERT 语句激活,也能被 LOAD DATA 语句激活。
DELETE: 从表中删除某一行数据时激活触发器,例如 DELETE 和 REPLACE 语句。
UPDATE:更改表中某一行数据时激活触发器,例如 UPDATE 语句。
2.3、BEFORE | AFTER
BEFORE 和 AFTER,触发器被触发的时刻,表示触发器是在激活它的语句之前或之后触发。若希望验证新数据是否满足条件,则使用 BEFORE 选项;若希望在激活触发器的语句执行之后完成几个或更多的改变,则通常使用 AFTER 选项。
2.4、表名
与触发器相关联的表名,此表必须是永久性表,不能将触发器与临时表或视图关联起来。在该表上触发事件发生时才会激活触发器。同一个表不能拥有两个具有相同触发时刻和事件的触发器。例如,对于一张数据表,不能同时有两个 BEFORE UPDATE 触发器,但可以有一个 BEFORE UPDATE 触发器和一个 BEFORE INSERT 触发器,或一个 BEFORE UPDATE 触发器和一个 AFTER UPDATE 触发器。
2.5、触发器主体
触发器动作主体,包含触发器激活时将要执行的 MySQL 语句。如果要执行多个语句,可使用 BEGIN…END 复合语句结构。
2.6、FOR EACH ROW
一般是指行级触发,对于受触发事件影响的每一行都要激活触发器的动作。例如,使用 INSERT 语句向某个表中插入多行数据时,触发器会对每一行数据的插入都执行相应的触发器动作。
注意:每个表都支持 INSERT、UPDATE 和 DELETE 的 BEFORE 与 AFTER,因此每个表最多支持 6 个触发器。每个表的每个事件每次只允许有一个触发器。单一触发器不能与多个事件或多个表关联。
另外,在 MySQL 中,若需要查看数据库中已有的触发器,则可以使用 SHOW TRIGGERS 语句。
3、创建一个插入数据的触发器-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 INTO `test`.`test_uuid`( `content`, `create_time`) VALUES ( '查看插入生成uuid', now());
可以看到uuid自动填充了。
边栏推荐
- The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
- Es classes and objects, prototypes
- Apprentissage avancé des fonctions en es6
- ORM model -- creation and query of data records
- There is a problem using Chinese characters in SQL. Who has encountered it? Such as value & lt; & gt;` None`
- Postman interface test IV
- 大整数类实现阶乘
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
- The physical meaning of imaginary number J
- Agile course training
猜你喜欢

Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT

Basic chapter: take you through notes

Programming features of ISP, IAP, ICP, JTAG and SWD

ORM -- query type, association query

Bean operation domain and life cycle

【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)

Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance

【acwing】786. 第k个数

web3.0系列之分布式存储IPFS

The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
随机推荐
ORM--查询类型,关联查询
Phpcms realizes PC website access to wechat native payment
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
Advanced function learning in ES6
为什么安装mysql时starting service报错?(操作系统-windows)
Interface test
ORM model -- creation and query of data records
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
STM32 ADC和DMA
AI moves from perception to intelligent cognition
ORM--数据库增删改查操作逻辑
[untitled]
CONDA creates virtual environment offline
Differences between MCU and MPU
XML配置文件解析与建模
Write it into the SR table in the way of flinksql. It is found that the data to be deleted has not been deleted. Refer to the document https://do
STM32 Basics - memory mapping
ES类和对象、原型
Postman interface test VI
The new activity of "the arrival of twelve constellations and goddesses" was launched