SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
DROP TABLE IF EXISTS employee; CREATE TABLE employee ( name text, salary int, last_change_role text, last_change_time timestamp ); INSERT INTO employee VALUES ('Aang', 1, NULL, NULL), ('Zuko', 99999, NULL, NULL); CREATE OR REPLACE FUNCTION employee_insert_row() RETURNS TRIGGER AS $$ BEGIN IF (NEW.name IS NULL OR NEW.salary IS NULL OR NEW.salary < 0) THEN RETURN NULL; END IF; NEW.last_change_role = current_user; NEW.last_change_time = current_timestamp; RETURN NEW; END $$ LANGUAGE 'plpgsql'; CREATE OR REPLACE TRIGGER employee_insert_trigger BEFORE INSERT OR UPDATE ON employee FOR EACH ROW EXECUTE FUNCTION employee_insert_row(); INSERT INTO employee VALUES ('Toph', 10000, NULL, NULL); UPDATE employee SET salary = -1 WHERE name = 'Zuko'; SELECT * FROM employee;

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear