quot;Instead ofquot; Triggers in mysql(“代替mysql中的触发器)
问题描述
我有一个客户.
CREATE TABLE `customer` (
`cNic` VARCHAR(50) NOT NULL DEFAULT '',
`cName` VARCHAR(100) NULL DEFAULT NULL,
`cAddress` VARCHAR(500) NULL DEFAULT NULL,
`cTp` VARCHAR(50) NULL DEFAULT NULL,
`cEmail` VARCHAR(50) NULL DEFAULT NULL,
`creditLimit` DOUBLE NULL DEFAULT NULL,
`CustomerStatus` INT(11) NULL DEFAULT '0',
PRIMARY KEY (`cNic`)
)
将新行添加到此客户表时,如果新行的 cNic
(主键)已在客户表中,我想对现有(旧)表进行更新与其他字段一起排.(更新客户集 cName='NewName' where cNic = 'enteredNic'
).
When a new row is added to this customer table, If the cNic
(primary key) of the new row is already in the customer table I want to do an update to the existing (old) row with the the other fields. (Update customer set cName='NewName' where cNic = 'enteredNic'
).
否则(如果 cNic
不在表中)插入一个新行.我知道我可以在 mssql 中使用 而不是
触发器来做到这一点.在mysql中有没有办法做到这一点.
Otherwise (If the cNic
is not in the table) insert a new row. I know I can do this in mssql using instead of
triggers. Is there is a way to do this in mysql.
推荐答案
MySQL 没有代替触发器,但它有 REPLACE 和 INSERT...ON DUPLICATE KEY UPDATE(参见文档).
MySQL does not have instead of triggers, but it does have REPLACE and INSERT...ON DUPLICATE KEY UPDATE (see documentation).
另见我对INSERT IGNORE"的回答;与在重复密钥更新时插入..."
这篇关于“代替"mysql中的触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“代替"mysql中的触发器


基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01