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

查看:63
本文介绍了“代替"在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(主键)已在客户表中,则我想与另一行对现有(旧)行进行更新领域. (Update customer set 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,则插入新行.我知道我可以使用instead of触发器在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没有触发器,但确实有在重复的键更新中插入... (请参阅文档).

MySQL does not have instead of triggers, but it does have REPLACE and INSERT...ON DUPLICATE KEY UPDATE (see documentation).

另请参阅我对"INSERT IGNORE"的回答;与"INSERT ... ON DUPLICATE KEY UPDATE"相对应;

这篇关于“代替"在mysql中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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