Created
October 12, 2021 06:29
-
-
Save ignavan39/9a8d43c7c1a20c1337575801716b0fa0 to your computer and use it in GitHub Desktop.
SQL snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION trigger_set_timestamp() | |
RETURNS TRIGGER AS $$ | |
BEGIN | |
NEW.updated_at = NOW(); | |
RETURN NEW; | |
END; | |
$$ LANGUAGE plpgsql; | |
ALTER TABLE users ADD created_at TIMESTAMP NOT NULL DEFAULT NOW(); | |
ALTER TABLE users ADD updated_at TIMESTAMP NOT NULL DEFAULT NOW(); | |
CREATE TRIGGER set_timestamp_to_user | |
BEFORE UPDATE ON users | |
FOR EACH ROW | |
EXECUTE PROCEDURE trigger_set_timestamp(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment