开发者

How to check if a table row exist with a trigger?

I want to check if a table row exist with a trigger. If it does then it does nothing, b开发者_如何学编程ut if it doesnt exist it inserts a row.

Here is my current code I want to modify:

BEGIN
IF (NEW.counter >= 100) THEN
// check if a certain row exists in another table, if not, execute the beneath code
INSERT INTO tagCategories (name, counter) VALUES ('unnamed', NEW.counter);
UPDATE tagCategories2tagPairs SET tagCategoryId = LAST_INSERT_ID() WHERE tagPairId = OLD.id;
END IF;
END

Is there any good tutorials on this, teaching all functions you can use with a trigger (they don't look like the php functions)?


mysql trigger tutorial

CREATE TRIGGER Syntax

For the restrictions on behaviour allowed in Triggers, please see:

D.1. Restrictions on Stored Routines, Triggers, and Events

To check if row exists and update it, otherwise insert a row if it does not exist, you can use the INSERT INTO ... ON DUPLICATE KEY UPDATE statement, but within the restrictions for triggers mentioned in the above link.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜