SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table usr ( id int8 not null, activation_code varchar(255), active boolean not null, email varchar(255), password varchar(255) not null, username varchar(255) not null, primary key (id) ); CREATE OR REPLACE FUNCTION usr_before_insert_or_update() RETURNS TRIGGER AS $$ DECLARE existing_user RECORD; BEGIN SELECT INTO existing_user * FROM usr WHERE username = NEW.username; IF FOUND THEN RAISE EXCEPTION 'Username already exists.'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER usr_before_insert_or_update_trigger BEFORE INSERT OR UPDATE ON usr FOR EACH ROW EXECUTE PROCEDURE usr_before_insert_or_update(); INSERT INTO usr (id, username, password, email) VALUES (7, 'newuser', 'newpassword', 'newuser@example.com'); SELECT * FROM usr WHERE username = 'newuser'; INSERT INTO usr (id, username, password, email) VALUES (8, 'existinguser', 'anotherpassword', 'another@example.com'); UPDATE usr SET password = 'newpassword', email = 'updated@example.com' WHERE username = 'existinguser'; SELECT * FROM usr WHERE username = 'existinguser'; UPDATE usr SET username = 'newuser' WHERE username = 'existinguser';

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear