waz
waz

Reputation: 41

sqlite3 triggers confusion with OLD and NEW

What is the difference between OLD.id , NEW.id and just id in sqlite3 triggers?

please help me with this got some explanation which never explained what old and new does

Upvotes: 0

Views: 521

Answers (1)

CL.
CL.

Reputation: 180280

The documentation says:

Both the WHEN clause and the trigger actions may access elements of the row being inserted, deleted or updated using references of the form "NEW.column-name" and "OLD.column-name", where column-name is the name of a column from the table that the trigger is associated with.

id alone can be used in an SELECT/INSERT/UPDATE/DELETE statement, and refers to a column in one of the tables used in that statement.

Typically, you would use OLD or NEW to find the row in the actual table:

CREATE TRIGGER ...
BEGIN
    UPDATE MyTable SET UpdateTime = time('now') WHERE id = NEW.id;
END;

Upvotes: 1

Related Questions