“代替"mysql中的触发器 [英] "Instead of" Triggers in mysql

查看:30
本文介绍了“代替"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 没有代替触发器,但它有 REPLACEINSERT...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中的触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆