“代替"mysql中的触发器

2022-01-01 00:00:00 sql mysql triggers

我有一个客户.

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"的回答;与在重复密钥更新时插入..."

相关文章